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

leetcode 345. Reverse Vowels of a String

时间:2018-06-21 00:05:46      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:put   ring   bre   self   pad   hello   rate   join   code   

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

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        arr = list(s)
        vows = {"a", "e", "i", "o", "u"}
        i, j = 0, len(arr)-1
        while i<j:
            if arr[i].lower() in vows:
                if arr[j].lower() in vows:
                    arr[i], arr[j] = arr[j], arr[i]                    
                    i += 1
                j -= 1
            else:
                i += 1
        return "".join(arr)

 or

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        arr = list(s)
        vows = {"a", "e", "i", "o", "u"}
        i, j = 0, len(arr)-1
        while i<j:
            if arr[i].lower() in vows:
                while i<j and arr[j].lower() not in vows:
                    j -= 1
                if i == j: break
                arr[i], arr[j] = arr[j], arr[i]                    
                j -= 1
            i += 1
        return "".join(arr)

 or 都用贪心:

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        arr = list(s)
        vows = {"a", "e", "i", "o", "u"}
        i, j = 0, len(arr)-1
        while i<j:
            while i<j and arr[i].lower() not in vows:
                i += 1
            while i<j and arr[j].lower() not in vows:
                j -= 1
            if i < j:
                arr[i], arr[j] = arr[j], arr[i]                    
                j -= 1
                i += 1
        return "".join(arr)

 或者是two sum的思路,

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        arr = list(s)
        vows = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
        i, j = 0, len(arr)-1
        while i<j:            
            l, r = arr[i] in vows, arr[j] in vows
            if l and r:                
                arr[i], arr[j] = arr[j], arr[i]                    
                j -= 1
                i += 1
            elif l and not r:
                j -= 1
            elif not l and r:
                i += 1
            else:
                i += 1
                j -= 1
        return "".join(arr)

 此外,还有使用stack存储vows的做法,两次遍历,第一次生成stack,第二次在遇到vows时候直接pop stack里的字符替换掉。

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        arr = list(s)
        vows = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
        stack = []
        for i,c in enumerate(arr):
            if c in vows:
                stack.append(c)
        for i,c in enumerate(arr):
            if c in vows:
                arr[i] = stack.pop()        
        return "".join(arr)

 

leetcode 345. Reverse Vowels of a String

标签:put   ring   bre   self   pad   hello   rate   join   code   

原文地址:https://www.cnblogs.com/bonelee/p/9206297.html

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