leetcode 345

来源:互联网 发布:淘宝是什么公司的 编辑:程序博客网 时间:2024/04/30 13:59

题目意思是让一个string类型中的元音字母前后调换顺序,典型的字符串处理问题,要注意的是原因字母可以是大写或者小写。

用两个指针一前一后遍历元音字母。

class Solution(object):    def reverseVowels(self, s):        """        :type s: str        :rtype: str        """        l = list(s)        llen = len(l)        t = ['a','e','i','o','u','A','E','I','O','U']        left,right = 0,llen-1        while left < right:            while l[left] not in t and left < right:                left += 1             while l[right] not in t and left < right:                right -= 1            #if left < right:            l[left],l[right] = l[right],l[left]            left += 1            right -= 1        s = ''.join(l)        return s

没想到的是效率特别低!!不明白为什么就找了别人的代码看,修改后唯一的变化:

t = {'a','e','i','o','u','A','E','I','O','U'}

突然就打败了92%的python提交的人的代码,我顿时惊呆了!!字典的效率为什么比list高这么多呢?

估计是字典的内部设计上的问题,有空再看看 

0 0