leetCode 345. Reverse Vowels of a String 字符串

简介:

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".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

思路:

找到元音字母,标记位置,进行置换。


代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// vowels(元音字母)包括:a,e,i,o,u. 
 
class  Solution {
public :
     bool  isVowels( char  c)
     {
         char  vowels[10] = { 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' };
         for ( int  i = 0; i < 10; i++)
         {
             if (c == vowels[i])
             {
                 return  true ;
                 break ; //return 和 break在一起都有反应么?
             }
         }
         return  false ;
     }
     string reverseVowels(string s) {
         vector< int > recordIndex;
         vector< char  > str;
         for ( int  i = 0;i<s.size();i++)
         {
             str.push_back(s.at(i));
         }
         
         for ( int  i = 0; i < str.size() ; i++)
         {
             if ( isVowels(str[i]) )
             {
                 recordIndex.push_back(i);
             }
         }
         
         for ( int  i = 0 ; i < recordIndex.size() / 2 ; i++)
         {
             char  left = str[recordIndex[i]];
             char  right = str[recordIndex[recordIndex.size() - 1 - i]];
             str[recordIndex[i]] = right;
             str[recordIndex[recordIndex.size() - 1 - i]] = left;
         }
         s =  "" ;
         
         for ( int  i = 0 ; i < str.size() ; i++)
         {
             s.append(1,str[i]);
         }
 
         return  s;
     }
};


本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1835538

相关文章
|
4天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
4天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
28天前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
204 92
|
2月前
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
198 14
|
5月前
|
Go 索引
【LeetCode 热题100】394:字符串解码(详细解析)(Go语言版)
本文详细解析了 LeetCode 热题 394:字符串解码。题目要求对编码字符串如 `k[encoded_string]` 进行解码,其中 `encoded_string` 需重复 `k` 次。文章提供了两种解法:使用栈模拟和递归 DFS,并附有 Go 语言实现代码。栈解法通过数字栈与字符串栈记录状态,适合迭代;递归解法则利用函数调用处理嵌套结构,代码更简洁。两者时间复杂度均为 O(n),但递归需注意栈深度问题。文章还总结了解题注意事项及适用场景,帮助读者更好地掌握字符串嵌套解析技巧。
141 6
|
6月前
|
数据处理
鸿蒙开发:ArkTs字符串string
字符串类型是开发中非常重要的一个数据类型,除了上述的方法概述之外,还有String对象,正则等其他的用处,我们放到以后得篇章中讲述。
299 19
|
6月前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
188 11
|
12月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
181 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行