String:345. Reverse Vowels of a String
来源:互联网 发布:淘宝冲q币怎么买 编辑:程序博客网 时间:2024/05/29 08:34
我的代码,很常规的方法:
class Solution {public: string reverseVowels(string s) { set<char> ss = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; int i = 0, j = s.size() - 1; while(i < j) { if(ss.find(s[i]) == ss.end()) i++; else if(ss.find(s[j]) == ss.end()) j--; else swap(s[i++], s[j--]); } return s; }};看到有一个人是这样写的:
class Solution {public: string reverseVowels(string s) { int i = 0, j = s.size() - 1; while (i < j) { i = s.find_first_of("aeiouAEIOU", i); j = s.find_last_of("aeiouAEIOU", j); if (i < j) { swap(s[i++], s[j--]); } } return s; }};这里面用到了str1.find_first_of(str2, pos). 这个函数的意思是,返回的是str2这个字符串中的任意一个首次在str1种出现的位置,从str1的pos位置之后开始找。
阅读全文
0 0
- String:345. Reverse Vowels of a String
- 345. Reverse Vowels of a String 【E】
- LeetCode 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- leetcode-345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- #leetcode#345. Reverse Vowels of a String
- [leetcode] 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- Leetcode:345. Reverse Vowels of a String
- LeetCode *** 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- LeetCode.345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- LeetCode 345. Reverse Vowels of a String
- celery heart-beat
- springmvc+spring+jdbc+oracle练习
- 关于网狐源码机器人添加
- VS2010、VS2013等VS中如何统计项目的代码行数
- Uploadcare如何构建每天处理350M文件API请求的服务堆栈
- String:345. Reverse Vowels of a String
- 3894: 文理分科
- selenium3.x系列篇之四-chromedriver
- Kafka学习整理五(Consumer配置)
- 100. Same Tree
- Android使用kotlin实现增量更新详细全过程
- 三国志
- Linux查看系统状态命令
- Markdown 语法说明(简体中文版)