Leetcode 15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
解法一:
这一题如果用解决combination的DFS思路,会导致出现很多重复解。比如见下图的例子,假设存在一个解是[-5,-2,7], 由于-2在list中出现了两次,所以这个解至少会出现两次。
1.可以先对nums进行排序。
2. 对于k in range(n), 我们考察[k+1,n]这个区间。由于要求三个数的和为零。所以nums[i]+nums[j]的target就是 -nums[k]。
2.1 如果 nums[i]+nums[j] == target, i 右移一格,j 左移一格。为了不出现重复解,下图的情况 i 要移动到不是-2为止。
2.2 如果 nums[i]+nums[j] < target, i 右移一格
2.3 如果 nums[i]+nums[j] > target, j 左移一格
L12是为了第一个数不出现重复的情况,所以如果nums[k] == nums[k+1], 那么k+1这一步就直接跳过。 另外和two sum不同的是,这里由于事先做了排序,所以并不需要建立一个Hash table。

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
res = []
n = len(nums)
for k in range(n):
if nums[k] > 0: break
if (k>0 and nums[k] == nums[k-1]): continue
target = 0 - nums[k]
i, j = k+1, n - 1
while i < j:
if nums[i] + nums[j] == target:
res.append([nums[k], nums[i], nums[j]])
while i<j and nums[i] == nums[i+1]:
i += 1
while i<j and nums[j] == nums[j-1]:
j -= 1
i += 1; j -= 1
elif nums[i] + nums[j] < target:
i += 1
else:
j -= 1
return res
Leetcode 15. 3Sum的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [LeetCode] 15. 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
随机推荐
- 运算符.png
- 关于IOS中safari下的select下拉菜单,文字过长不换行的问题
今天遇到下图这种问题,文字过长,显示不全.折腾了老半天,在网上搜了半天也找不到解决方案. 于是问了下同事,同事提到了<optgroup>,这个标签厉害. <optgroup> ...
- Android Weekly Notes Issue #221
Android Weekly Issue #221 September 4th, 2016 Android Weekly Issue #221 ARTICLES & TUTORIALS And ...
- iOS - 捕获应用程序崩溃日志
作为一名iOS移动应用开发者,为了确保你的应用程序正确无误,在将应用程序提交到应用商店之前,你必定会进行大量的测试工作:而且在你测试的过程中应用程序运行的很好,但是在应用商店上线之后,还是有用户抱怨应 ...
- iOS 学习 - 2.据网址显示源码
输入网址,解出源码,显示label 我这里是在第二个界面显示的,用的属性传值. A界面先从 storyboard 拖个 textfield 和一个 button .m里面button的方法 //按钮点 ...
- iOS App 不支持http协议 App Transport Security has blocked a cleartext HTTP (http://)
目前iOS已经不支持http协议了,不过可以通过info.plist设置允许 App Transport Security has blocked a cleartext HTTP (http://) ...
- the request resource is not available
form表单递交数据的问题 我的解决方法 将要访问的servlet地址写入form的action中 例如:访问地址为http://localhost:8080/Webprojectselfservic ...
- 4、项目的培训 - PMO项目管理办公室
培训是一个重要的内容,在公司内部就有相关的培训.对于PMO项目管理办公室来说,就是相关的项目的技术和业务的培训,以期让项目组人员能够快速的学习好项目业务内容和所需要使用到的技术内容,然后尽快的进入项目 ...
- j2ee项目服务器怎样部署?
1.右击项目 >> 点击如图1中❶ >> Project(选择项目) >> Add 如图1: 图1 2.点击Add >> Server ...
- CSS3 基于关系的选择器
常见的基于关系的选择器 选择器 选择的元素 A E 元素A的任一后代元素E (后代节点指A的子节点,子节点的子节点,以此类推) A > E 元素A的任一子元素E(也就是直系后代) E:first ...