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

    Note:
    The vowels does not include the letter “y”.

    class Solution {
    public:
        string reverseVowels(string s) {
            vector<char>ch;
            for(int i=0;i<s.length();++i)
            {
                if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
                        ch.push_back(s[i]);
            }
            reverse(ch.begin(),ch.end());
            int k = 0;
            for(int i=0;i<s.length();++i)
            {
                if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
                        s[i] = ch[k++];
            }
            return s;
        }
    };
  • 相关阅读:
    luogu P1451 求细胞数量
    P1443 马的遍历
    luogu P1194 买礼物
    codevs 4919 线段树练习4
    printf的实型
    printf的整型
    scanf
    printf
    c++常用函数
    字符类型C++(ascll码表)
  • 原文地址:https://www.cnblogs.com/chankeh/p/6850087.html
Copyright © 2011-2022 走看看