LeetCode 345 反轉字串中的母音字母 (Reverse Vowels of a String)
用一個容器按序填入所有所有的母音,並用一個字元替換。再將容器逆序輸出填到對應的字元上即可實現。程式碼如下:
class Solution {
public:
string reverseVowels(string s) {
vector<char> yuan;
for(int i = 0; i < s.size(); i++ )
{
if(s[i] == 'a' ||s[i] == 'e' ||s[i] == 'i' ||s[i] == 'o' ||s[i] == 'u'
|| s[i] == 'A' ||s[i] == 'E' ||s[i] == 'I' ||s[i] == 'O' ||s[i] == 'U')
{
yuan.push_back(s[i]);
s[i] = '$';
}
}
int size = yuan.size();
for(int i = 0; i < s.size(); i++ )
{
if(s[i] == '$')
{
s[i] = yuan[size-1];
size--;
}
}
return s;
}
};
相關推薦
【LeetCode】345. 反轉字串中的母音字母(Reverse Vowels of a String)
【 英文練習 | 中文練習 】 題目描述: 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例: 輸入: "hello" 輸出: "holle" 解題思路: 雙指標典型題目,注意母音字母不要只考慮小寫的。 public String reverseVowe
C#LeetCode刷題之#345-反轉字串中的母音字母(Reverse Vowels of a String)
問題 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 輸入: "hello" 輸出: "holle" 輸入: "leetcode" 輸出: "leotcede" 說明:母音字母不包含字母"y"。 Write a function that tak
LeetCode 345 反轉字串中的母音字母 (Reverse Vowels of a String)
用一個容器按序填入所有所有的母音,並用一個字元替換。再將容器逆序輸出填到對應的字元上即可實現。程式碼如下: class Solution { public: string reverseVowels(string s) { vector&
leetcode 557. 反轉字串中的單詞 III(Reverse Words in a String III)
給定一個字串,你需要反轉字串中每個單詞的字元順序,同時仍保留空格和單詞的初始順序。 示例 1: 輸入: "Let's take LeetCode contest" 輸出: "s'teL ekat edoCteeL tsetnoc" 注意:在字串中,每個單詞由單個空格分
leetcode (Reverse Vowels of a String)
Title:Reverse Vowels of a String 345 Difficulty:Easy 原題leetcode地址:https://leetcode.com/problems/reverse-vowels-of-a-string/ 1.
【LeetCode】151. 翻轉字串裡的單詞(Reverse Words in a String)
【 英文練習 | 中文練習 】 題目描述: 給定一個字串,逐個翻轉字串中的每個單詞。 示例: 輸入: "the sky is blue" 輸出: "blue is sky the" 說明: 無空格字元構成一個單詞。 輸入字串可以在前面或者後面包含多餘的
【LeetCode】345.Reverse Vowels of a String(反轉字串中的母音字母)-C++實現
本題為谷歌面試題。 問題描述: 一、第一種方法:對撞指標法 #include <iostream> #include <vector> #include <string> #include <cassert> #inc
LeetCode刷題記錄——第345題(反轉字串中母音字母)
題目描述 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例 1: 輸入: “hello” 輸出: “holle” 示例 2: 輸入: “leetcode” 輸出: “leotcede” 思路分析 將所有母音字母放
LeetCode 345. 反轉字串中的母音字母(java 實現)
編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例 1: 輸入: "hello" 輸出: "holle" 示例 2: 輸入: "leetcode" 輸出: "leotcede" 說明: 母音字母不包含字母"y"。 //雙指標 class Solution
Leetcode 345. 反轉字串中的母音字母 By Python
編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例 1: 輸入: "hello" 輸出: "holle" 示例 2: 輸入: "leetcode" 輸出: "leotcede" 說明: 母音字母不包含字母"y"。 思路 設立2個指標,一個從索引0開始向右,一個從末尾向前,根據條件進
leetcode 345.反轉字串中的母音字母
題目描述: 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 思路:從前往後找到一個母音字母,再從後往前找到一個母音字母,然後把他們對換;持續這個操作,直到前後兩個方向遍歷到同一個位置。由於每個元素都訪問一次,複雜度應該是O(n)。 inline bool isVowel(
Leetcode 345. 反轉字串中的母音字母 Java&Python
編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例 1: 輸入: "hello" 輸出: "holle" 示例 2: 輸入: "leetcode" 輸出: "leotcede" 說明: 母音字母不包含字母"y"。 這道題目的解題思路與167號問題兩數之和一致,這裡
LeetCode 345. 反轉字串中的母音字母
編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。示例 1:給定 s = "hello", 返回 "holle".示例 2:給定 s = "leetcode", 返回 "leotcede".注意:母音字母不包括 "y".//思路 :把所有的母音字母都入棧,然後第二次遍歷
LeetCode 345. 反轉字串中的母音字母(C++)
題目: 編寫一個函式,以字串作為輸入,反轉該字串中的母音字母。 示例 1: 給定 s = “hello”, 返回 “holle”. 示例 2: 給定 s = “leetcode”, 返回 “leotcede”. 注意: 母音字母不包括 “y”.
leetcode 345 Reverse Vowels of a String(翻轉支付串中的母音字母)
題目要求 寫一個函式,實現對於給定的字串中的母音字母進行翻轉。 注意輸入均為小寫,或者大寫字母。 演示示例 Example 1 Input: “hello” Output: “holle” Example 2 Input: “leetcode” Output: “l
課外題:讀取字串中母音字母(包含大小寫)的個數
法一:getchar函式,讀到行末,逐一檢測 #include<stdio.h> int main(){ char c; while((c=getchar())!='\n'){ if(c=='A'||c=='E'||c=='I'||c=='O
[LeetCode] Reverse Vowels of a String 翻轉字串中的母音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s =
[Swift]LeetCode345. 反轉字串中的母音字母 | Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Inpu
345. Reverse Vowels of a String | 逆置字串中的母音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". E
345 Reverse Vowels of a String 反轉字符串中的元音字母
class ble otc -s else 字符串 反轉字符串 一個 -a 編寫一個函數,以字符串作為輸入,反轉該字符串中的元音字母。示例 1:給定 s = "hello", 返回 "holle".示例 2:給定 s = "leetcode", 返回 "leotcede".