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

LeetCode 345. Reverse Vowels of a String

时间:2016-09-20 23:56:48      阅读:142      评论: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".

题目要求:将一个字符串中的元音字母前后调换,设置两个位置,如果两个位置的都是元音字母就调换即可

 

class Solution {
public:
    string reverseVowels(string s) {
        char temp =  ;
        string Vowels("aAeEiIoOuU");
        int start = 0, end = s.size() - 1;
        while (start<end)
        {
            if (Vowels.find(s[start]) != string::npos&&Vowels.find(s[end]) != string::npos)
            {
                temp = s[start];
                s[start] = s[end];
                s[end] = temp;
                ++start;
                --end;
            }else 
            {
                if (Vowels.find(s[start]) == string::npos && Vowels.find(s[end]) == string::npos)
                {
                    ++start;
                    --end;
                    continue;
                }
                if (Vowels.find(s[start] == string::npos) && Vowels.find(s[end]) != string::npos)
                {
                    ++start;
                }
                else
                {
                    --end;
                }
            }
        }
        return s;
    }
};

 

LeetCode 345. Reverse Vowels of a String

标签:

原文地址:http://www.cnblogs.com/csudanli/p/5890771.html

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