类型:字符串
-
- 反转字符串中的元音字母 ****💚
https://leetcode-cn.com/problems/reverse-vowels-of-a-string/
❓ 编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
💡 双指针
class Solution: def isVowel(self, c): return c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] def reverseVowels(self, s: str) -> str: ls = list(s) left, right = 0, len(ls) - 1 while True: while left < len(ls) and not self.isVowel(ls[left]): left += 1 while right >= 0 and not self.isVowel(ls[right]): right -= 1 if left >= right: break else: ls[left], ls[right] = ls[right], ls[left] left += 1 right -= 1 return ''.join(ls)时间复杂度:O(n),空间复杂度:O(1)