编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"

示例 2:

输入: "leetcode"
输出: "leotcede"

说明:

元音字母不包含字母"y"。


思路

设立2个指针,一个从索引0开始向右,一个从末尾向前,根据条件进行处理即可

代码

class Solution:
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
yuan = ['a','e','i','o','u','A','E','I','O','U']
s = list(s) l = 0
r = len(s)-1
while l < r:
if s[l] in yuan and s[r] in yuan:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1
elif s[l] in yuan and s[r] not in yuan:
r -= 1
elif s[l] not in yuan and s[r] in yuan:
l += 1
else:
l += 1
r -= 1
return ''.join(s)

Leetcode 345. 反转字符串中的元音字母 By Python的更多相关文章

  1. Java实现 LeetCode 345 反转字符串中的元音字母

    345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...

  2. LeetCode:反转字符串中的元音字母【345】

    LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...

  3. 【leetcode 简单】 第八十三题 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  4. 345 Reverse Vowels of a String 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...

  5. [Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...

  6. leetCode题解之反转字符串中的元音字母

    1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...

  7. Python实现 "反转字符串中的元音字母" 的方法

    #coding=utf- def reverseVowels(s): """ :type s: str :rtype: str """ sS ...

  8. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  9. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

随机推荐

  1. Comet OJ 热身赛(E题)(处理+最短路算法)

    dijkstra 已经提交 已经通过 42.86% Total Submission:189 Total Accepted:81 题目描述 Eagle Jump公司正在开发一款新的游戏.泷本一二三作为 ...

  2. iOS悬浮窗口(无论界面跳转、View始终在视图上显示,可移动)

    2016.09.24 23:52* 字数 71 阅读 5925评论 9喜欢 11 让所有界面都显示,最好还是封装一个继承Window的类:JYCWindow. 先看看效果:   mygif.gif 关 ...

  3. Python_阻塞IO、非阻塞IO、IO多路复用

    0.承上 进程: 计算机里最小的资源分配单位: 数据隔离, 利用多核,数据不安全. 线程: 计算机中最小的CPU调度单位: 数据共享,GIL锁,数据不安全. 协程: 线程的一部分,是有用户来调度的; ...

  4. HDU 2001 计算两点间的距离

    http://acm.hdu.edu.cn/showproblem.php?pid=2001 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离 ...

  5. php之常用扩展总结

    在此总结,开发中经常使用到的扩展,来进行日常PHP的开发工作 bcmath(精确数值处理) bz2 calendar Core ctype curl date dom ereg exif filein ...

  6. Python3练习题 006 冒泡排序

    import random a = [random.randint(1,100) for i in range(10)]def bu(target): length = len(target) whi ...

  7. [转帖]How To Be Successful

    How To Be Successful http://blog.samaltman.com/how-to-be-successful 总结一下文章的重点: 1. Compound yourself2 ...

  8. smarTTY总是失败连接的原因

    首先用命令 IP addr 查看是否ip 地址错误  事实证明就是因为我的ip地址发生了变化所以导致连接不上, 不过有一次,我将电脑重启 也是连接上了的.

  9. Ubuntu中MySql的启动与关闭

    安装mysql sudo apt-get install mysql-server sudo apt install mysql-client sudo apt install libmysqlcli ...

  10. Excel文件读取的两种方式

    1.Pandas库的读取操作 from pandas import read_excel dr=read_excel(filename,header) dr#dataframe数据 dw=DataFr ...