题目描述:

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. (转)排列算法 Permutation Generation

    转自:http://www.cnblogs.com/dragonpig/archive/2010/01/21/1653680.html http://www.notesandreviews.com/p ...

  2. Simulate a seven-sided die using only five-sided

    问题描述: 如题 转述一下问题,就是说你现在有一个正五面体骰子,然后你怎么用这个正五面体骰子去模拟一个正七面体骰子. 这个问题我接触到几种方法,下面一一阐述. 方法一: rand7()=( rand5 ...

  3. POJ 3185

    The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4088   Accepted: 1609 D ...

  4. sql只修改第一二行数据

    update t_table set colname=*  where a=1 order by id desc limit 1,2

  5. lintcode:整数排序||

    题目 给一组整数,按照升序排序.使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法. 解题 归并排序 public class Solution { /** * @param ...

  6. CF 253B Two Heaps

    #include<stdio.h> #include<algorithm> #include<map> using namespace std; struct No ...

  7. Discuss!X3.2 绑定微信

    完整程序 http://pan.baidu.com/s/1jGL5veQ 密码kgga Discuz!X3.2 在继承和完善 Discuz!X3.1 的基础上,针对社区移动端进行了新的尝试.新版本主要 ...

  8. Java科普之基础知识回顾

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Java语言由C衍生,丢弃C中的指针,使用"发出指令-接收返回值-验证-发出指令-接收返回 ...

  9. c语言结构体赋值问题

    对于结构体赋值问题: static psl{ int a; char ch; }; 我过去一般会对结构体这样赋值: static psl pslname = { , 'b', }; 记住有一点,‘b’ ...

  10. oracle11g OEM无法连接到数据库实例解决办法

    我的电脑是32位的win7家庭版系统,那么这样的系统能不能装上oracle呢?能的!就是可能会出错,在装oracle时,每个人遇到的问题都不同,有的人装了双系统,有的人重做了系统,真心酸,先让电脑断网 ...