leetcode 3Sum python
# sort the array
# loop from i = 0
# then left=i+1 right=len(nums)-1
# try nums[i] - ( nums[left]+nums[right]) = 0
# class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res=list()
nums.sort()
if len(nums) <= 2:
return res for index in range(len(nums)-2):
if index == 0 or nums[index] > nums[index-1]:
left=index+1
right=len(nums)-1
while left < right:
if nums[left] + nums[right] == -nums[index]:
res.append( [nums[index],nums[left],nums[right] ] )
left+=1
right-=1
#ensure no repeat value
while left<right and nums[left] == nums[left-1]:
left+=1
while left < right and nums[right] == nums[right+1]:
right-=1
elif nums[left] + nums[right] < -nums[index]: while left < right:
left+=1
if nums[left] != nums[left-1]:
break
else:
while left < right:
right-=1
if nums[right] != nums[right+1]:
break
return res
@link http://www.cnblogs.com/zuoyuan/p/3699159.html
leetcode 3Sum python的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- Prefabs实例化 ResourceMgr
http://www.xiaobao1993.com/886.html 来源与小宝个人笔记[稍作修改] //使用 Prefabs/Resources/stone1 ResourceMgr.GetIn ...
- Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN(http://smartblack.iteye.com/blog/1129097)
Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN 在ByteArray和Socket中,能看到一个属性endain. endian : String 更改或读取数据的字节顺 ...
- 3D 转换
用X.Y.Z分别表示空间的3个维度,三条轴互相垂直.如下图 1.左手坐标系 2.透视(perspective) 电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现 ,通过 ...
- css3文本效果
CSS3 包含多个新的文本特性. 在本章中,您将学到如下文本属性: 1. text-shadow 2. word-wrap 浏览器支持 Internet Explorer 10.Firefox.Chr ...
- Android 上多方式定位元素(python)
Android 上多方式定位元素(python) 在学习实际UI自动化测试的时候,首先就需要定位元素,然而定位元素也是最头疼的地方,因为元素各种控件名称的缺失会影响元素的准确定位.下面针对Androi ...
- 如何让tableView展示数据
设置数据源对象 self.tableView.dataSource = self; 数据源对象要遵守协议 @interface ViewController () <UITableViewDat ...
- mysql和VS2010 C++链接过程中出现的问题
PS:二者连接的过程主要参考这篇博客园文章,http://www.cnblogs.com/justinzhang/archive/2011/09/23/2185963.html 这篇博客园的文章中的代 ...
- mysql优化(2) 观察mysql进程状态
环境准备,安装sysbench wget http://nchc.dl.sourceforge.net/project/sysbench/sysbench/0.4.12/sysbench-0.4.12 ...
- jquery1.11 操作checkbox:全选、取消全选、获取选择元素、获取取消选择元素(总结)
jquery1.11.1版本完成对checkbox的操作 1. 使用属性prop设置选中状态 2.使用:checked和:not(:checked)获取选中的元素 源码: <!DOCTYPE h ...
- lua学习笔记(1)-基本语法
==============变量类型nilnumber(实数) 1 2 3.14 7.65e8string "hello world" "\n ...