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 文中代码为了不动官网提供的初始 ...
随机推荐
- 使用FileSystemWatcher捕获系统文件状态
源代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- C# 获取类似java gettime() 的时间格式
今天做了一个面向Java的接口,需要做到时间的统一,C#提供了System.DateTime.UtcNow 但是需要自己做下处理,记录一下自己的方法, 留着以后查阅方便. /// <summar ...
- C++写geohash
http://www.cnblogs.com/LBSer/p/3310455.html http://www.sxrczx.com/pages/my.oschina.net/853294317/blo ...
- tcp窗口滑动以及拥塞控制
转自:http://blog.chinaunix.net/uid-26275986-id-4109679.html TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥 ...
- PHP编程中10个最常见的错误
PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.本篇经将为大家介绍PHP开发中10个最常见的问题,希望能够对朋友有所帮助. 错误1:foreach循环后留下 ...
- MySQL的三层架构之一----连接层
1.mysql的服务端可以分为三层,分别是连接层,SQL层,存储层. 2.架构图 3.连接层定义了通信server端与client协议:
- Oracle EBS-SQL (PO-11):检查采购订单退货数.sql
select msi.segment1 物料编码, -- msi.inventory_item_id return_it ...
- mysql_healthly
cat mysql_healthly.php <?php if (!defined('IN_PDK')){ define('IN_PDK', true); } $db_name = $_GET[ ...
- CC++初学者编程教程(11) 配置Windows数据库服务器
1.我们新建一个虚拟机. 2. 选择默认的WorkStation10.0. 3.我们选择VS2012的镜像. 4.我们设置用户密码,跳过WindowsSever2012密钥 5.我们选择是,稍后手动激 ...
- 利用 Windows Azure 实现“云优先”
根据 IDC 的调查,云计算无疑为我们的合作伙伴提供了巨大的机会,预计到 2016 年,全球企业将在公共云服务上耗资 980 亿美元.在今天的休斯敦全球合作伙伴大会上,我们非常高兴能与合作伙伴共同寻求 ...