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 文中代码为了不动官网提供的初始 ...
随机推荐
- Linux下更新瑞芯微固件
有这样的需求,是因为提供的BSP中是在windows下烧写的系统.我直接在linux下开发,每次编译后代码都要在windows下烧写,是一个很麻烦的事情.为了解决这个问题我尝试着用VirtualBox ...
- AjaxHelper学习,ajax,microsoft,mvc,asp.net
index.cshtml @using (Ajax.BeginForm("ContentAjax", new AjaxOptions { UpdateTargetId = &quo ...
- q.js实现nodejs顺序调用
nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise 什么是promise promise一个标准,它描述了异步调用的返回结果,包括正确 ...
- iOS8毛玻璃效果
UIBlurEffect*blueEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView* ...
- 安装nginx 做反向代理
nginx反向代理配置实例(前nginx+后apache)Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP ...
- Scala基础入门-代码碎片
import scala.util.control._ import java.util.Date object Test { def main(args: Array[String]) { // v ...
- python运维开发(六)----模块续
内容目录 反射 模块 os模块 sys模块 md5加密模块 re正则匹配模块 configparse模块 xml模块 shutil模块 subprocess模块 反射 利用字符串的形式去对象(模块)中 ...
- 学习笔记-menusript控件中条目权限设置使用
在做一个小程序的时候,偶然发现了使用menusript控件做权限设置的方法,仅此标记,以供参考. 首先创建一个实例:testuseright.sln, 在项目文件里创建两个窗体:Form1.cs和us ...
- C++箴言:理解 new-handler的行为
当 operator new 不能满足一个内存分配请求时,它抛出一个 exception(异常).很久以前,他返回一个 null pointer(空指针),而一些比较老的编译器还在这样做.你依然能达到 ...
- Struts2 一、 视图转发跳转
<struts> <constant name="struts.118n.encoding" value="UTF-8"></co ...