作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/reverse-vowels-of-a-string/

Total Accepted: 7758 Total Submissions: 22132 Difficulty: Easy

题目描述

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Input: "hello"
Output: "holle"

Example 2:

Input: "leetcode"
Output: "leotcede"

Note:

  • The vowels does not include the letter “y”.

题目大意

把一个字符串中所有的元音字母倒序,其他位置不变。

解题方法

使用栈

理解题意很重要啊!

这个题的意思是把收尾向中间走的时候遇到的所有元音字符换位置。也就是说 “abecui”–>“ibucea”;

把某个东西进行翻转,很容易想到栈。所以把元音字符进栈,再次遍历的时候遇到元音字符就出栈即可。

class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
vstack = []
for c in s:
if c in "aeiouAEIOU":
vstack.append(c)
res = []
for c in s:
if c in "aeiouAEIOU":
res.append(vstack.pop())
else:
res.append(c)
return "".join(res)

双指针

也就是用双指针的方法。一个从头查找,一个从尾查找。同时判断是否为元音字符,如果两个指针都是落在了元音字符上的时候,交换。别忘了交换位置之后前往下一个地点。

python代码如下:

class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
N = len(s)
res = list(s)
left, right = 0, N - 1
while left < right:
while right >= 0 and res[right] not in "aeiouAEIOU":
right -= 1
while left < right and res[left] not in "aeiouAEIOU":
left += 1
if left < right:
res[left], res[right] = res[right], res[left]
left += 1
right -= 1
return "".join(res)

Java代码如下:

public class Solution {
public String reverseVowels(String s) {
ArrayList<Character> list=new ArrayList();
list.add('a');
list.add('e');
list.add('i');
list.add('o');
list.add('u');
list.add('A');
list.add('E');
list.add('I');
list.add('O');
list.add('U'); char[] array=s.toCharArray(); int head=0;
int tail=array.length-1; while(head<tail){
if(!list.contains(array[head])){
head++;
continue;
}
if(!list.contains(array[tail])){
tail--;
continue;
}
char temp=array[head];
array[head]=array[tail];
array[tail]=temp; head++;
tail--;
} return new String(array);
}
}

AC:11ms

C++代码如下:

class Solution {
public:
string reverseVowels(string s) {
const int N = s.size();
int left = 0, right = N - 1;
while (left < right) {
while (left < N && !isVowel(s[left])) left ++;
while (right >= 0 && !isVowel(s[right])) right --;
if (left < right)
swap(s[left], s[right]);
left ++;
right --;
}
return s;
}
private:
bool isVowel(char x) {
string t = "aeiouAEIOU";
return t.find(x) != string::npos;
}
};

日期

2016/5/1 20:52:19
2018 年 11 月 21 日 —— 又是一个美好的开始
2018 年 12 月 4 日 —— 周二啦!

【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)的更多相关文章

  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】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

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

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

  8. 345. Reverse Vowels of a String - LeetCode

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

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

随机推荐

  1. RabbitMQ消息中介之Python使用

    本文介绍RabbitMQ在python下的基本使用 1. RabbitMQ安装,安装RabbitMQ需要预安装erlang语言,Windows直接下载双击安装即可 RabbitMQ下载地址:http: ...

  2. [云原生]Docker - 安装&卸载

    目录 系统要求 卸载旧版本 安装Docker 方法一:通过repo安装 设置Repository 安装Docker Engine 升级Docker Engine 方法二:通过package安装 方法三 ...

  3. java打jar包和运行jar包的两种方式

    java打jar包和运行jar包的两种方式更详细的打包方式请参考https://www.cnblogs.com/mq0036/p/8566427.html 一.java类不依赖第三方jar包以简单的一 ...

  4. Linux系统分区及挂载点

    一.关于Linux的分区情况 虽然硬盘分区表中最多能存储四个分区,但我们实际使用时一般只分为两个分区,一个是主分区(Primary Partion)一个是扩展分区(extended partition ...

  5. Linux_spool命令

    spool的作用是什么? spool的作用可以用一句话来描述:在sqlplus中用来保存或打印查询结果. 参数指南 对于SPOOL数据的SQL,最好要自己定义格式,以方便程序直接导入,SQL语句如: ...

  6. 编译安装nginx 1.16

    准备源码包,并解压,创建nginx用户 [root@slave-master ~]# tar xf nginx-1.16.0.tar.gz [root@slave-master ~]# useradd ...

  7. Mybatis通用Mapper介绍与使用

    前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL.而且,当数据库表结构改动时,对应的所有SQ ...

  8. redis的总结笔记

    # Redis    1. 概念: redis是一款高性能的NOSQL系列的非关系型数据库        1.1.什么是NOSQL            NoSQL(NoSQL = Not Only ...

  9. SpringCloud技术涵盖简介

    SpringCloud是微服务架构的集大成者,云计算最佳业务实践. 我们平常使用的Spring和他们的关系,对Spring,springboot , SpringCloud 的 概念区分,上图: Sp ...

  10. Java oop 笔记

    摘要网址:http://note.youdao.com/noteshare?id=bbdc0b970721e40d327db983a2f96371