classSolution: defreverseVowels(self, s: str) -> str: vowels = 'aeiouAEIOU' start = 0 end = len(s) - 1 while start < end: while s[end] notin vowels and start < end: end -= 1 while s[start] notin vowels and start < end: start += 1 if s[start] in vowels and s[end] in vowels: s[start], s[end] = s[end], s[start] start += 1 end -= 1 return''.join(s)
字符串操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defreverseVowels(self, s: str) -> str: s = list(s) vowels = 'aeiouAEIOU' ans = [] for i in s: if i in vowels: ans.append(i) a = '' for i inrange(len(s)): if s[i] in vowels: a += ans.pop() else: a += s[i] return''.join(a)