zoukankan      html  css  js  c++  java
  • 345. Reverse Vowels of a String

    1. 问题描述

    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".
    Tags: Two Pointers String
    Similar: Problems (E) Reverse String

    2. 解题思路


    3. 代码

    class Solution {
    public:
        string reverseVowels(string s) 
        {
            string::size_type iLeft = -1;
            string::size_type iRight = s.size();
            while (true)
            {
                iLeft = FindVowels(s, iLeft+1);
                iRight = rFindVowels(s, iRight-1);
                if (iLeft < iRight && iLeft != s.npos && iRight != s.npos)
                {
                    char ch = s[iLeft];
                    s[iLeft] = s[iRight];
                    s[iRight] = ch;
                }
                else
                {
                    break;
                }
            }
            return s;
        }
        
    private:
        int FindVowels(const string &s, int iStartPos)
        {
            string strVowels = "aAoOeEiIuU";
            string::size_type pos = s.find_first_of(strVowels, iStartPos);
            return pos;
        }
    
        int rFindVowels(const string &s, int iStartPos)
        {
            string strVowels = "aAoOeEiIuU";
            string::size_type pos = s.find_last_of(strVowels, iStartPos);
            return pos;
        }
    };

    4. 反思

  • 相关阅读:
    Hadoop命令手册
    HDFS配额管理指南
    HDFS权限管理用户指南
    Hadoop分布式文件系统使用指南
    Hadoop分布式文件系统:架构和设计
    ImageLoader 图片加裁
    发送 一个无序广播
    Intent 转向
    Volley Get Post 方法
    Android 动态设置控件宽高度
  • 原文地址:https://www.cnblogs.com/whl2012/p/5820065.html
Copyright © 2011-2022 走看看