一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

(二)解题

题目大意:将字符串中的元音字母反转位置。

解题思路:用两个指针i和j,分别从头尾开始向中间扫描,每当碰到一个元音字母就停下来,交换位置,然后两个继续向中间扫描。

具体思路看代码。

class Solution {
public:
    string reverseVowels(string s) {
        int i =0;
        int j =s.length()-1;
        while(i<=j)
        {
            while((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')) i++;//找到第一个元音字母时停止
            while((s[j]!='a')&&(s[j]!='e')&&(s[j]!='i')&&(s[j]!='o')&&(s[j]!='u')&&(s[j]!='A')&&(s[j]!='E')&&(s[j]!='I')&&(s[j]!='O')&&(s[j]!='U')) j--;//找到第一个元音字母时停止
            if(i<=j){//如果i<=j,则交换位置
                char temp = s[i];
                s[i] = s[j];
                s[j] = temp;
                i++;j--;//继续往中间扫描
            }
        }
        return s;
    }
};

【一天一道LeetCode】#345. Reverse Vowels of a String的更多相关文章

  1. Python [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 ...

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

  3. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...

  4. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

  5. Leetcode 345 Reverse Vowels in a String

    两个for 第一个for将每一个元音依次存放进一个char数组 第二个for,每检测到元音,就从char数尾部开始,依次赋值 如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择 hashs ...

  6. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  7. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  8. 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 v ...

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

随机推荐

  1. github常用命令

    全局配置 git config --global user.name "lewiscutey"git config --global user.email "lewisc ...

  2. 中断API之setup_irq【转】

    转自:https://blog.csdn.net/tiantao2012/article/details/78957472 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  3. Map 探索

    关于Map问题我一直了解的不是很透彻,下面进行比较系统的总结一下. 1.

  4. Linux 基本概念和操作

    我们在使用Linux时,不是直接和系统打交道,而是通过shell的中间程序.在图形界面下为了实现窗口的输入和输出,linux系统为我们提供了终端模拟器Terminal,常见的终端模拟器有 gnome- ...

  5. Python中模块之xml的讲解

    xml模块的功能介绍 这里主要讲解xml模块下的etree.ElementTree类. 1. 创建 具体代码如下 import xml.etree.ElementTree as XM namelist ...

  6. DELL、HP、IBM X86服务器命名规则

    DELL.HP.IBM X86服务器命名规则 各大服务器厂家对于自己的服务器命名都有一定的规则,通常会根据服务器的外观(如塔式.机架式.刀片等).处理器(如Intel或者AMD等).架构等信息来命名. ...

  7. three.js 3D 动画场景

    Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机.光影.材质等各种对象.使用它它能让 WebGL 变得更加简单. 下面用Three.js渲染一个物体360 ...

  8. text-size-adjust: none并没有什么用

    在样式文件中添加text-size-adjust: none,并没有什么用,移动端浏览器自动调整字体大小还是在进行,webkit已取消对其支持,移动端避免浏览器因为横屏.竖屏模式自动缩放字体大小可以加 ...

  9. Prometheus(转载)

    Prometheus 系统监控方案 一 https://www.cnblogs.com/vovlie/p/Prometheus_CONCEPTS.html 最近一直在折腾时序类型的数据库,经过一段时间 ...

  10. Linux的哲学思想

    1.一切皆文件:2.单一目的的小程序:3.组合小程序完成复杂任务:4.文本文件保存配置信息:5.尽量避免捕获用户接口:6.提供机制,而非策略. 说到底Linux的哲学思想在于方便和更好的管理后台,不同 ...