345. Reverse Vowels of a String*

来源:互联网 发布:淘宝模特快速摆拍 编辑:程序博客网 时间:2024/06/05 08:23

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:

The vowels does not include the letter "y".

My code

class Solution(object):    def reverseVowels(self, s):        """        :type s: str        :rtype: str        """      vowelList=['A','a','O','o','E','e','I','i','U','u']        lowIndex = 0        highIndex = len(s)-1        s= list(s)        while lowIndex<=highIndex:           if s[lowIndex] in vowelList and s[highIndex] in vowelList:               s[lowIndex],s[highIndex]=s[highIndex],s[lowIndex]               lowIndex +=1               highIndex -=1           elif s[lowIndex] not in vowelList and s[highIndex] in vowelList:               lowIndex +=1           elif s[lowIndex]  in vowelList and s[highIndex] not in vowelList:               highIndex -=1           else:               lowIndex +=1                        highIndex -=1            return ('').join(s)
Notes:

s[lowIndex],s[highIndex]=s[highIndex],s[lowIndex]
('').join(s



0 0
原创粉丝点击