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 ...
随机推荐
- Oracle Sales Cloud:管理沙盒(定制化)小细节2——使用对象触发器更新数字字段
在上一篇 "管理沙盒(定制化)小细节1" 的随笔中,我们使用公式法在 "业务机会" 对象(单头)上建立了 "利润合计" 字段,并将它等于 & ...
- Atitit 《控制论原理与概论attilax总结
Atitit <控制论原理与概论attilax总结 <控制论> 奠基之作,出自创始人维纳.虽然内容权威,但我认为带有相当强烈的个人色彩,且门槛较高,不适合入门.深入研究控制论必看书籍 ...
- ContentProvider中央档案馆,以及获取联系人电话的示例
Android官方文档介绍的数据存储方式共有五种,sqlite,SharedPreferences,网络存储,外储存储,文件存储,但是这些数据都无法进行共享,那么我们就引入了今天的主角:Content ...
- Android应用项目中BaseAdapter、SimpleAdapter和ArrayAdapter中的三种适配器
一.写在前面: 本次我们来讲解一下Android应用中三个适配器:BaseAdapter.SimpleAdapter和ArrayAdapter.其中常见的是BaseAdapter,也是个人推荐使用的适 ...
- ASP.NET MVC Routing、Areas、URLs
webForm页面运行起来url一般是这样的:localhost:****/index.aspx,这个过程就是当你运行页面的时候,vs开发工具自带的微型服务器会打开你存在硬盘上的这个文件然后显示在浏览 ...
- Eclipse使用Maven创建web3.0项目
安装Maven插件 这一步不细说了,自己下载的Eclipse-JAVA EE 版已自带 Maven插件 开始创建 文本1New一个 Maven Web App项目:File-->New--> ...
- Hadoop技巧系列索引
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink Hadoop技巧(01):插件,终端权限 Had ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- ubuntu kylin 14.04安装配置MongoDB v2.6.1(转)
1.获取最新版本 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz 2.解压并进入bin目录 tar zxvf mongo ...
- 安卓使用SQlite3数据库无法id主键无法自动增加?不是的。
安卓使用SQlite3数据库无法id主键无法自动增加?不是的. 要这样写:id integer primary key ,要写integer而不是int所以会报错! http://blog.csdn. ...