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
- leetcode 345
- leetcode 345
- [LeetCode] 344&345
- Leetcode no. 345
- leetcode 14, 38, 345
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- 【HDU 1599】find the mincost route 【最小环】
- 基类指针和子类指针相互赋值
- java 计算两个数的百分比,以百分号显示
- Hadoop概述
- RGB到HSI转换
- leetcode 345
- android 事件分发机制
- Validate Binary Search Tree
- 数据存储之SwiftJSON
- Android布局优化之include、ViewStub、merge
- android 动态获得资源ID
- 一天一条Linux指令-usleep
- 第三个问题:看看我们的项目编译后文件夹
- win7关联句柄轻松删除“正在使用的文件”