Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Input: "hello"
Output: "holle"
Example 2:
Input: "leetcode"
Output: "leotcede"
Note:
The vowels does not include the letter "y".
代码:
class Solution: def reverseVowels(self, s): """ :type s: str :rtype: str """ check = "aeiouAEIOU" s_list = list(s) keep = [] for key, value in enumerate(s_list): if value in check: s_list[key] = None keep.append(value) keep.reverse() for key, value in enumerate(s_list): if value == None: if (keep): s_list[key] = keep.pop(0) return "".join(s_list)
1.可以看到使用enumerate使得在索引编号时更加方便
2..'join()可以使列表变成字符串