作者: 负雪明烛
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 日 —— 周二啦!

最新文章

  1. wordpress 自定义登录表单
  2. Java 的静态代理 动态代理(JDK和cglib)
  3. Ubuntu里的若干问题解决方案
  4. 关于快捷键 Ctrl+Alt+[方向键] 的知识
  5. C语言语法之运算符及优先级
  6. 验证你的邮箱是不是qq邮箱
  7. mac 安装使用 webp 来压缩图片
  8. php把时间格式化
  9. opencv在VS2010命令行编译过程
  10. Aptana Studio 3 官方汉化包汉化
  11. UNIX网络编程——网络数据包检测
  12. python进阶学习(四)
  13. 《深入理解Java虚拟机》-----第8章 虚拟机字节码执行引擎——Java高级开发必须懂的
  14. MVC RedirectToAction 跳转时传参问题
  15. CF418D Big Problems for Organizers
  16. [UE4]InterpToMovement
  17. 《mysql必知必会》学习_第六章_20180730_欢
  18. canvas 实现签名效果
  19. WARNING: The host &#39;r6&#39; could not be looked up with /usr/local/mysql/bin/resolveip.
  20. soanr - 企业用户角色管理

热门文章

  1. perl和python3 同时打开两个文件
  2. vector去重--unique
  3. kubernetes部署haproxy、keepalived为kube-apiserver做集群
  4. 阿里云NAS性能测试
  5. C#gridview颜色提示
  6. Yarn的Tool接口案例
  7. MapReduce02 序列化
  8. 大数据学习day18----第三阶段spark01--------0.前言(分布式运算框架的核心思想,MR与Spark的比较,spark可以怎么运行,spark提交到spark集群的方式)1. spark(standalone模式)的安装 2. Spark各个角色的功能 3.SparkShell的使用,spark编程入门(wordcount案例)
  9. android studio 使用 aidl(二)异步回调
  10. @Transactional注解详细使用