345.Reverse Vowel of a String

Posted bernieloveslife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了345.Reverse Vowel 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".

class Solution:
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        temp = []
        s = list(s)
        for i in s:
            if i in [‘a‘,‘e‘,‘i‘,‘o‘,‘u‘,‘A‘,‘E‘,‘I‘,‘O‘,‘U‘]:
                temp.append(i)
        temp.reverse()
        pos = 0
        for i in range(len(s)):
            if s[i] in [‘a‘,‘e‘,‘i‘,‘o‘,‘u‘,‘A‘,‘E‘,‘I‘,‘O‘,‘U‘]:
                s[i] = temp[pos]
                pos += 1
        return ‘‘.join(s)



以上是关于345.Reverse Vowel of a String的主要内容,如果未能解决你的问题,请参考以下文章

345 Reverse Vowels of a String 反转字符串中的元音字母

345. Reverse Vowels of a String

LeetCode 345. Reverse Vowels of a String

345. Reverse Vowels of a String

345. Reverse Vowels of a Stringeasy

LeetCode #345 Reverse Vowels of a String