leetcode Reverse Vowels of a String 反转字符串中的母音
2017-10-01 14:46
866 查看
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”.
题意:反转字母母音,第一个和最后一个互换,第二个和倒数第二个互换,依次类推。
思路:使用find_first_of 和find_last_of,find_first_of查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos 。
Example 1:
Given s = “hello”, return “holle”.
Example 2:
Given s = “leetcode”, return “leotcede”.
Note:
The vowels does not include the letter “y”.
题意:反转字母母音,第一个和最后一个互换,第二个和倒数第二个互换,依次类推。
思路:使用find_first_of 和find_last_of,find_first_of查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos 。
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; } };
相关文章推荐
- LeetCode Reverse Vowels of a String(字符串中元音字符反转)
- LeetCode 345 Reverse Vowels of a String(字符串操作)
- Leetcode 345 Reverse Vowels of a String 字符串处理
- 【leetcode】 Reverse Vowels of a String(翻转字符串中出现的元音字母)
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
- 【leetcode】(Python)Reverse Vowels of a String实现字符串内元字符倒序
- leetcode 345 Reverse Vowels of a String
- leetcode-Reverse Vowels of a String
- leetcode -- 541. Reverse String II 【字符串反转 + 双指针 + 状态记录 + 数组与堆内存】
- [leetcode, python] Reverse Words in a String 反转字符串
- leetcode_c++: Reverse Vowels of a String(345)
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- [leetcode, python] Reverse Words in a String 反转字符串
- leetcode 345:Reverse Vowels of a String
- LeetCode 345:Reverse Vowels of a String
- leetcode---Reverse Vowels of a String
- Leetcode - Reverse Vowels of a String
- leetcode 345 Reverse Vowels of a String
- leetcode之Reverse Vowels of a String
- [LeetCode][JavaScript][Python]Reverse Vowels of a String