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

个人博客:http://www.cnblogs.com/wdfwolf3/

这道题在逆置字符串的基础上加入了限制,只做原音(aeiouAEIOU)的逆置,不理会非原因的字符。在这里我主要针对如何判断原因讲述几个方法,至于字符串逆置不再讨论,可以看我专门写Reverse String的博文http://www.cnblogs.com/wdfwolf3/p/5484675.html

1.调用函数判断(16ms)

这个最基本最容易想到实现,没什么难度。

class Solution {
public:
    string reverseVowels(string s) {
        int i=0,j=s.length()-1;
        while(i<j)
        {
            while((isVowel(s[i])==false)&&(i<j))
            {
                i++;
            }
            while((isVowel(s[j])==false)&&(i<j))
            {
                j--;
            }
            swap(s[i],s[j]);
            i++;
            j--;
        }
        return s;
    }
    bool isVowel(char c)
    {
        if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U'))
            return true;
        return false;
    }
};
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
  • 2021-06-02
  • 2021-09-04
  • 2021-07-31
  • 2021-06-23
猜你喜欢
  • 2022-12-23
  • 2022-01-14
  • 2021-07-08
  • 2021-06-30
  • 2021-10-30
  • 2022-02-13
相关资源
相似解决方案