LeetCode #15 3Sum
2015-07-12 15:44
381 查看
class Solution: # @param {integer[]} nums # @return {integer[][]} def threeSum(self, nums): if nums == None: return None if len(nums) == 0: return [] nums.sort() intList = [] for i in range(0,len(nums)): #repeating of first element if i-1 >= 0 and nums[i] == nums[i-1]: continue p = i + 1 q = len(nums) -1 while p>=0 and p<len(nums) and q>=0 and p<q: if nums[i] + nums[p] + nums[q] == 0: triple = [nums[i],nums[p],nums[q]] intList.append(triple) p+=1 q-=1 #repeating of last two elements while nums[p-1] == nums[p] and p<len(nums) and p<q: p+=1 while nums[q] == nums[q+1] and q>=0 and p<q: q-=1 elif nums[i] + nums[p] + nums[q] > 0: q-=1 else: p+=1 return intList
相关文章推荐
- HDU — 3416 Marriage Match IV(最大流+最短路)
- WINCE驱动程序快速入门
- 经管学习笔记(1)
- Performance冷启动简单测试
- HDU 1892 See you~
- 编译器错误消息: CS0006: 未能找到元数据文件
- C#值类型与引用类型
- Android开发(三十二)——延时
- 屏幕适配
- Five Invaluable Techniques to Improve Regex Performance
- Five Invaluable Techniques to Improve Regex Performance
- ORACLE开启/关闭归档模式
- Five Invaluable Techniques to Improve Regex Performance
- Android开发(三十一)——重复引用包错误Conversion to Dalvik format failed
- Android开发(二十九)——layout_weight的含义
- 杭电 hdu 1033 (水题) 但英文特难,题意很难理解
- Palindrome Linked List
- 黑马程序员 集合类
- C语言中函数
- ubuntu将命令写在一个文件里,执行文件,source命令