345. Reverse Vowels of a String
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".
思路:
- low:双次循环,记住位置,然后替换
代码:python3
class Solution:
def reverseVowels(self, s: str) -> str:
arr = ['a','e','i','o','u','A','E','I','O','U']
low = 0
high = len(s)
for i in range(len(s)-1):
if s[i] in arr:
low = i
for j in range(high-1,low,-1):
if s[j] in arr:
high=j
print(i)
print(j)
s=s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:]
break
return s
复制代码
转载于:https://juejin.im/post/5cee3948f265da1b6028e5f5