码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】345. Reverse Vowels of a String 解题小结

时间:2016-09-13 00:10:05      阅读:137      评论: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".

 应该算不上有难度。

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 解题小结

标签:

原文地址:http://www.cnblogs.com/Doctengineer/p/5866768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!