lc345. Reverse Vowels of a String

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".

Ideas:

  1. low: double cycles, remember the location, and then replace

Code: 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
复制代码

Reproduced in: https: //juejin.im/post/5cee3948f265da1b6028e5f5

Guess you like

Origin blog.csdn.net/weixin_34242658/article/details/91456997