您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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".
思路:
找到元音字母,标记位置,进行置换。
代码如下:
// 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; } };
2016-08-08 10:52:10
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。