zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    };

  • 相关阅读:
    LeetCode:25 K个一组翻转链表
    LeetCode:3 无重复字符的最长子串(双指针)
    Java——参数问题与final实例域
    Java——对象的构造
    配置远程服务器 安装iis 远程服务器网络无法连接
    未能找到元数据文件
    ef 设计model 标签
    visualsvn for vs2017 初始化错误
    resharper 2018.2.3破解
    C# winform 自定义函数中找不到Form中的控件和定义的全局变量
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5425411.html
Copyright © 2011-2022 走看看