首页 > 代码库 > 【LeetCode】345. Reverse Vowels of a String 解题小结
【LeetCode】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".
应该算不上有难度。
class Solution {public: string reverseVowels(string s) { string str = s; for (int i = 0, j = str.size() - 1; i < j; ) { if (isVowel(str[i]) && isVowel(str[j])) { char tmp = str[i]; str[i] = str[j]; str[j] = tmp; i++; j--; } if ( !isVowel(str[i]))++i; if ( !isVowel(str[j])) --j; } return str; } bool isVowel(char c) { if (c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘u‘ || c == ‘o‘ || c == ‘A‘ || c == ‘E‘ || c == ‘I‘ || c == ‘U‘ || c == ‘O‘) return true; return false; }};
【LeetCode】345. Reverse Vowels of a String 解题小结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。