首页 > 其他 > 详细

345. Reverse Vowels of a String

时间:2017-10-27 11:08:34      阅读:344      评论:0      收藏:0      [点我收藏+]

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

如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同

 

C++(9ms):

 1 class Solution {
 2 public:
 3     string reverseVowels(string s) {
 4         int i = 0 ;
 5         int j = s.size() - 1 ;
 6         while(i < j){
 7             i = s.find_first_of("aeiouAEIOU",i) ;
 8             j = s.find_last_of("aeiouAEIOU",j) ;
 9             if (i < j){
10                 swap(s[i++],s[j--]) ;
11             }
12         }
13         return s ;
14     }
15 };

 

345. Reverse Vowels of a String

原文:http://www.cnblogs.com/mengchunchen/p/7742048.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!