345. 反转字符串中的元音字母-----leetcode刷题(
2019-11-06 本文已影响0人
吉祥鸟hu
[TOC]
题目
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
思路:
略微有点繁琐
- 将输入的字符串另存转换为一个列表
- 获取到全部的元音字母的位置,将其存到一个列表中
- 调换位置
- 将列表转换为字符输出
leetcode解题
class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
yuan=["a","e","i","o","u","A","E","I","O","U"]
s1=list(s)
num_list=[]
for zz,i in enumerate(s):
if i in yuan:
num_list.append(zz)
yuan1=0
yuan2=len(num_list)-1
while yuan1 < yuan2:
s1[int(num_list[yuan2])],s1[int(num_list[yuan1])]=s[int(num_list[yuan1])],s[int(num_list[yuan2])]
yuan1+=1
yuan2-=1
return "".join(s1)