我是靠谱客的博主 无奈唇彩,这篇文章主要介绍Leetcode_2_String_345反转字符串里的元音字母,现在分享给大家,希望可以做个参考。

这题有几处关键的地方:

1.先用list(s)函数将字符串转换成字符串数组

2.元音字母集合大小写都要收纳进去

3.最后的结果要用"".join()拼接。

执行用时 :88 ms, 在所有 Python3 提交中击败了59.82%的用户

内存消耗 :14.6 MB, 在所有 Python3 提交中击败了18.57%的用户

Code:

class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels=['a','e','i','o','u','A','E','I','O','U']
        s=list(s)
        left,right=0,len(s)-1
        while left<right:
            if s[left] in vowels and s[right] in vowels:
                temp=s[left]
                s[left]=s[right]
                s[right]=temp
                left+=1
                right-=1
            else:
                if s[left] not in vowels:left+=1
                if s[right] not in vowels:right-=1
        return "".join(s)

最后

以上就是无奈唇彩最近收集整理的关于Leetcode_2_String_345反转字符串里的元音字母的全部内容,更多相关Leetcode_2_String_345反转字符串里内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(131)

评论列表共有 0 条评论

立即
投稿
返回
顶部