leetcode345——Reverse Vowels of a String(C++)

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;
    }
};

2.利用数组模拟哈希表的方式(12ms)

class Solution {
public:
    string reverseVowels(string s) {
        int dict[128] = {0};
        dict[‘a‘]=1;
        dict[‘A‘]=1;
        dict[‘e‘]=1;
        dict[‘E‘]=1;
        dict[‘i‘]=1;
        dict[‘I‘]=1
        dict[‘o‘]=1;
        dict[‘O‘]=1;
        dict[‘u‘]=1;
        dict[‘U‘]=1;
        int i=0,j=s.length()-1;
        while(i<j)
        {
            while((dict[s[i]]==0)&&(i<j))
            {
                i++;
            }
            while((dict[s[j]]==0)&&(i<j))
            {
                j--;
            }
            swap(s[i],s[j]);
            i++;
            j--;
        }
        return s;
    }
};

3.利用字符串查找函数string.find()或者string.find_first_of()。关于这两个函数在最后面详细介绍。

class Solution {
public:
    string reverseVowels(string s) {
        string vowel="aeiouAEIOU";
        int i=0,j=s.length()-1;
        while(i<j)
        {
            while((vowel.find(s[i])==string::npos)&&(i<j))
            {
                i++;
            }
            while((vowel.find(s[j])==string::npos)&&(i<j))
            {
                j--;
            }
            swap(s[i],s[j]);
            i++;
            j--;
        }
        return s;
    }
};

(12ms)

string vowel="aeiouAEIOU";
        int i=0,j=s.length()-1;
        while(i<j)
        {
            i=s.find_first_of(vowel,i);
            j=s.find_last_of(vowel,j);
            if(i>=j)
                break;
            swap(s[i],s[j]);
            i++;
            j--;
        }
        return s;

(13ms)

P.S.

1.str1.find(str2, , )

作用是在str1中查找str2的位置,str2可以是单个字符,字符串变量或者字符串;第二个参数是str1的起始查找位置,即从str1的哪个位置开始查找,默认为0;第三个参数是只查找str2的前多少个字符,默认是str2的全部。可以没有后两个参数。返回的是str2首个字符在str1的位置,如果没有找到的话返回string::npos,它有几层含义,本身它是一个常量-1,当作为返回值时表示查找失败;在使用下标时,它大于任何下标(逻辑上概念),可以看作字符串的尽头或者说结尾。

2.string.find_first_of(str, , )

参数作用同上。但是函数作用是不同的,返回的值是str中任何一个字符首次在string中出现的位置。上一个函数相当于匹配,这个更像筛选。

3.string.find_last_of(str, , )

参数作用同上,作用也同上。区别是这个是向前查找的,从第二个参数位置开始向前找到str中任何一个字符首次在string中出现的位置,自然默认参数为string::npos。

时间: 2024-12-29 09:40:21

leetcode345——Reverse Vowels of a String(C++)的相关文章

345. Reverse Vowels of a String(C++)

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"

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【leetcode80】Reverse Vowels of a String(元音字母倒叙)

题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: 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 = "leet

LeetCode | Reverse Words in a String(C#)

题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 代码: public static string reverseWords(string str) { string reverStr = ""; int count = 0; Stack stac

Reverse Words in a String (4)

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word?A sequence of non-space characters constitutes a word.//单词中间没有空格 Could t

[LeetCode][JavaScript][Python]Reverse Vowels of a String

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". 将字符串中

345. Reverse Vowels of a String【easy】

345. Reverse Vowels of a String[easy] 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&q

LeetCode_345. Reverse Vowels of a String

345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" N

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,