#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".

 

题意:将一个字符串中a,e,i,o,u收尾互换位置,返回。

 

class Solution {
public:
    string reverseVowels(string s) {
        int len = s.length();
        char temp;
        for(int i = 0, j = len - 1; i < j; i++) {
            if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ||s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U') { 
            	for(j; j > i; j--)
            	if(s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u' ||s[j] == 'A' || s[j] == 'E' || s[j] == 'I' || s[j] == 'O' || s[j] == 'U') {
					temp = s[i];
					s[i] = s[j];
					s[j] = temp;
					j--;
					break;
				}
			} 
		} 
        return s;
    }
};

  

posted @ 2016-05-05 21:28  梦幻之海  阅读(192)  评论(0编辑  收藏  举报