码迷,mamicode.com
首页 > 其他好文 > 详细

345.Reverse Vowel of a String

时间:2018-11-05 16:25:15      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:temp   only   cti   end   leo   include   let   return   range   

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

标签:temp   only   cti   end   leo   include   let   return   range   

原文地址:https://www.cnblogs.com/bernieloveslife/p/9771761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!