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 文中代码为了不动官网提供的初始 ...
随机推荐
- 张冬:OpenPOWER CAPI为什么这么快?(二)
张冬:OpenPOWER CAPI为什么这么快?(二) PMC公司数据中心存储架构师张冬 有了CAPI的FPGA是怎么做的? 首先认识一下这个体系里的三个角色: AFU(Acceleration ...
- 微信朋友圈分享js代码最新2015年无错版
最近微信对分享做了进一步规范,导致很多分享都不起作用了,今天跟大家分享,2015年最新修无错的! 以下是主要微信分享页面代码:(其中红色部分主要懒友自己填写自己哈.) <?php require ...
- springmvc 传递和接收数组参数
java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...
- 触碰jQuery:AJAX异步详解(转)
AJAX 全称 Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).它并非一种新的技术,而是以下几种原有技术的结合体. 1) 使用CSS和X ...
- win server 2003 将MBR转为GPT突破硬盘2TB的限制(附微软磁盘科普)
备注:https://technet.microsoft.com/zh-cn/library/cc773223.aspx GUID 分区表 与支持最大卷为 2 TB (terabytes) 并且每个磁 ...
- Sql Server数据库设计高级查询
-------------------------------------第一章 数据库的设计------------------------------------- 软件开发周期: (1 ...
- Python成长之路第一篇(4)_if,for,while条件语句
有了以上的基本基础,已经上面写的几个小练习,大家肯定有很多的不满,比如查询为什么查询一次就退出了呢?下面我们来学习条件语句 一.万恶的加号 以前我们在print的时候如果要加上变量都有是使用+来作为连 ...
- python 中去除BOM头
在window的环境下,保存的文本文档会加上三个字符0xEF 0xBB 0xBF的头部,这三个字符可能会影响对文本的读取,形成乱码,在这里记录下如何避免. 首先发现直接保存为ASCII的文本文件是不包 ...
- [转]前端利器:SASS基础与Compass入门
[转]前端利器:SASS基础与Compass入门 SASS是Syntactically Awesome Stylesheete Sass的缩写,它是css的一个开发工具,提供了很多便利和简单的语法,让 ...
- css案例学习之盒子模型
定义:每个盒子都有:边界.边框.填充.内容四个属性: 每个属性都包括四个部分:上.右.下.左:这四部分可同时设置,也可分别设置:里的抗震辅料厚度,而边框有大小和颜色之分,我们又可以理解为生活中所见盒子 ...