Leetcode题目: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".

题目解答:

要求将字符串中所有的元音字母逆转,辅音字母的位置不受改变。

代码如下:

class Solution {
public:
    string reverseVowels(string s) {
        list<char> s_copy;
        list<char> s_vowels;
        int len = s.length();
        for(int i = 0;i < len;i++)
        {
            if(isVowel(s[i]))
            {
               s_vowels.push_back(s[i]);
               s_copy.push_back(‘a‘);
            }
            else
            {
                s_copy.push_back(s[i]);
            }
        }
        s_vowels.reverse();
        stringstream res;
        list<char>::iterator voit = s_vowels.begin();
        for(list<char>::iterator lit = s_copy.begin(); lit != s_copy.end();lit++)
        {
            if(*lit == ‘a‘)
            {
                res << *voit;
                voit++;
            }
            else
            {
                res << *lit;
            }
        }
        return res.str();
    }
   
    bool isVowel(char c)
    {
        switch(c)
        {
            case ‘a‘:
            case ‘e‘:
            case ‘i‘:
            case ‘o‘:
            case ‘u‘:
            case ‘A‘:
            case ‘E‘:
            case ‘I‘:
            case ‘O‘:
            case ‘U‘:
                return true;
            default:
                return false;
        }
    }
};

时间: 2024-10-08 20:57:29

Leetcode题目:Reverse Vowels of a String的相关文章

LeetCode 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". Note:The vowels does not include

【Leetcode】Reverse Vowels of a String

题目链接:https://leetcode.com/problems/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

[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(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

LeetCode OJ - Reverse Words in a String

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 解题思路: 1.先对字符串进行一次总的翻转 2.以空格为单位,划分单词,然后对每个单词再进行一次翻转 代码: class Solution { public: void swap(char& a, char

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] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

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】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();