一天一道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. box-sizing position

    box-sizing 属性 用于更改用于计算元素宽度和高度的默认的 CSS 盒子模型.可以使用此属性来模拟不正确支持CSS盒子模型规范的浏览器的行为. /* 关键字 值 */ box-sizing: ...

  2. DS4700电池更换步骤

    DS4700电池更换步骤: 在A控制器里操作(带电热插拔控制器)对逻辑盘进行切换: (需要先将A控下挂的硬盘手工切换到B控上.然后对A控进行电池更换,更换完成后再将原A控下挂硬盘切回) 如下详细步骤: ...

  3. [原创]手把手教你写网络爬虫(7):URL去重

    手把手教你写网络爬虫(7) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 本期我们来聊聊URL去重那些事儿.以前我们曾使用Python的字典来保存抓取过的URL,目的是将重复抓取的UR ...

  4. 给div添加2个class

    <div class='center nocontent'>  类名用空格分开 在优先级一样的情况下就近原则  优先级# 100 .10 <span>这种标签是1 所以的相加 ...

  5. WEB中间件--tomcat爆破,burp和python脚本

    1.tomcat 用burpsuit进行弱口令爆破 先抓包 发送到inturder payload type 选择custom iterater 第一个payload选用户名文件,第二个payload ...

  6. SSM框架原理,作用及使用方法(非原创)

    原帖:地址https://blog.csdn.net/bieleyang/article/details/77862042 如有侵权请联系删除 作用: SSM框架是spring MVC ,spring ...

  7. Printer for Me

    今天,良心系部终于给我配了打印机^^. 办公室门外还挂了牌子.

  8. vue.js-路由

    1:编写router.js   import Router from "vue-router" import Vue from "vue" import rou ...

  9. 阿里云linux下web服务器配置

    markdown截图不方便,本教程不用markdown编写 首先参考文章 https://www.jianshu.com/p/2604e53a7f6a?from=singlemessage 安装完后无 ...

  10. Go 语言结构体

    Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比如保存图书馆的书籍记录,每 ...