题目描述:

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

解题思路:

设置两个指针,分别从前往后与从后往前移动

代码如下:

class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
vowels = set(list('aeiouAEIOU'))
s = list(s)
start = 0
end = len(s) - 1 while start <= end:
if s[start] in vowels and s[end] in vowels :
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
else:
if s[start] not in vowels:
start += 1
if s[end] not in vowels:
end -= 1 return ''.join(s)

  

Python [Leetcode 345]Reverse Vowels of a String的更多相关文章

  1. 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 字符串处理

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

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

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

  4. Leetcode 345 Reverse Vowels in a String

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

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

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

  6. 345. Reverse Vowels of a String - LeetCode

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

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

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

  9. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

随机推荐

  1. POJ 1845 Sumdiv (求某个数的所有正因子的和)

    题意: 求A^B的所有正因子的和,最后模9901的结果. 思路: 若对一个数n进行素数分解,n=p1^a1*p2^a2*p3^a3*...*pk^ak那么n的所有正因子之和sum=(1+p1+...+ ...

  2. setTimeout延时0毫秒的作用和问题

    一 作用 http://www.cnblogs.com/xieex/archive/2008/07/11/1241151.html 经常看到setTimeout延时0ms的javascript代码,感 ...

  3. jquery select处理

    JQuery 绑定select标签的onchange事件,弹出选择的值,并实现跳转.传参 js 处理 select :选中,删除,更改等 http://blog.csdn.net/wust_star/ ...

  4. POJ 1300 Door Man(欧拉回路的判定)

    题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉 ...

  5. Linux网络编程3——socket

    宏定义 首先介绍两个宏定义,看如下代码 代码1 /************************************************************************* & ...

  6. oci.dll文件是用来干嘛的? 如果没有安装ORACLE客户端提示oci.dll未加载

    oracle数据库开发编程中,没有找到oci.dll,一般是系统的 path 设置有问题, 查找oci.dll, 然后加入到系统路径.oci.dll 可下载解压到系统盘的system32目录下.然后打 ...

  7. Win8.1安装VirtualSVN Server发生service visualSVN Server failed to start解决办法

    Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for mo ...

  8. 301. Remove Invalid Parentheses

    题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...

  9. python流程控制语句 ifelse - 4

    嵌套 #! /usr/bin/python a = int(input ("请输入一个整数:")) : : print ("无法显示") else : prin ...

  10. USACO Section 2.4: Fractions to Decimals

    乍看题目感觉有难度,实际分析后其实是道简单题 /* ID: yingzho1 LANG: C++ TASK: fracdec */ #include <iostream> #include ...