编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello" 输出: "holle"
示例 2:
输入: "leetcode" 输出: "leotcede"
说明:
元音字母不包含字母"y"。
题目标签:Two Pointers / String
题目链接:LeetCode / LeetCode中国
将字符串转为字符数组:list(s)
,不必[c for c in s]
Language | Runtime | Memory |
---|---|---|
python3 | 96 ms | N/A |
class Solution:
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
ss = [c for c in s]
tmp = []
for i, c in enumerate(ss):
if c.lower() in 'aeiou':
tmp.append((i, c))
for ii, (i, c) in enumerate(tmp):
ss[i] = tmp[-1 * ii -1][1]
return ''.join(ss)