LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium
题目:
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.
翻译:
给定一个n个整数的数组S,S中是否存在有a b c三个元素使得a+b+c=0?在S中找到所有的非重复组合,使它们的和为0
注意:答案集不能包含重复的数组。例如:[1,5,-6]与[-6,5,1]等数组,答案只出现其中一个
思路:三。。。个。。for循环??

(⊙x⊙;)。。。就要写!
Code:311 / 313 test cases passed. —— Time Limit Exceeded 时间复杂度:O(N3)
public static List<List<Integer>> threeSum1(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> outList = new ArrayList<List<Integer>>();
for (int i = 0; i < nums.length; i++) {
if ((i > 0 && nums[i] == nums[i - 1])) // 各循环起始点不需要判断重复
continue; // 不用i++ 的原因:避免最后的k还要增加边界判断,进入下一个循环则会自动边界判断“i < nums.length”
for (int j = i + 1; j < nums.length; j++) {
if ((j > i + 1 && nums[j] == nums[j - 1]))
continue; // 当有很多for和if的时候,条件取反后用continue,以此取代if的{}缩进,使代码可读性增加
for (int k = j + 1; k < nums.length; k++) {
if ((k > j + 1 && nums[k] == nums[k - 1]))
continue;
if ((nums[i] + nums[j] + nums[k] == 0)) {
List<Integer> inList = new ArrayList<Integer>();
inList.add(nums[i]);
inList.add(nums[j]);
inList.add(nums[k]);
outList.add(inList);
break;
}
}
}
}
return outList;
}
(因为只有在尾巴进行添加并无其他功能,所以采用ArrayList比较实惠和效率)
312测试用例超时:
[82597,-9243,…………此处省略N千个数字]
不想说话,让蠢人静静。。。。
————————————————————————智商分割线————————————————————————
参考答案:74ms——beats85.12% 时间复杂度O(N2)
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for (int i = 0; i < nums.length-2; i++) {
int left = i + 1;
int right = nums.length - 1;
if (i > 0 && nums[i] == nums[i - 1])
continue; // 去掉重复的起点
while (left < right) {
int sum = nums[left] + nums[right] + nums[i];
if (sum == 0) {
result.add(Arrays.asList(new Integer[]{nums[i], nums[left], nums[right]}));
while (left < right && nums[left] == nums[left + 1])
left++; // 去掉重复的左点
while (left < right && nums[right] == nums[right - 1])
right--; // 去掉重复的右点
right--; // 进入下一组左右点判断
left++;
} else if (sum > 0) {
right--; // sum>0 ,说明和过大了,需要变小,所以移动右边指针
} else {
left++; // 同理,需要变大,移动左指针
}
}
}
return result;
}
精髓在于: (排序 + 去重) + 双指针移动相向定位
注意:不需要“重复的双胞胎数组”,所以是“组合”即Cn/m(从M个数里随机选出N个数有多少种情况)而不是排列An/m——考虑去重
所查找的数之间具有一定的关联(本题为:和为0),那么就应该利用这个属性,而不是简单的进行搜索。

思路解析:
首先考虑数组遍历时去重:方法一:先将数组排好序,在遍历的时候与上一个进行比较,相同则直接进入下一个
方法二:用容器Set——简单,但是同样需要排序,增加算法复杂度并且此题三个数操作不方便【在第一题TwoSum的后面有更新用Set的方法】。
所以数组遍历去重在已排序的情况下优先采取方法一。
其次考虑降低多次循环算法复杂度:
降低复杂度一般的途径就是利用已有的而未用到的条件将多余的步骤跳过或者删去!
由于三个数是具有一个特点的:和为某个定值,这个条件只是用来判断了而并没有使用
并且,由上个去重得知,后面使用的数组是已经排序好的。
此时仔细想想应该就能想到
从两端使用两个指针相向移动,两端指针所指数之和如果小于目标值,只需要移动左边的指针,否则只需要移动右边的指针!!
例如[1,1,1,4,5,7,8,8,9]中 定目标值为15,从两边开始,1+9为10,小于15,移动右边指针左移变成1+8只会更少,所以移动左边变成4+9以此类推。。
妈耶!我想不到啊!

此时为两数定位,而题目中要找3个数
………………外嵌for循环!!!又回到了我的领域 ahhhhhhh~
分析搞定!
LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium的更多相关文章
- 【LeetCode每天一题】3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- [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] 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.数组.16最接近的三数之和-java
1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...
- [leetcode]15. 3Sum三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- [LintCode] 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 OJ:Three Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
随机推荐
- 网站漏洞扫描工具(appscan,mdcsoft-ips)
网站漏洞扫描工具:主要应用网站漏洞扫描工具,其原理是通过工具通过对网站的代码阅读,发现其可被利用的漏洞进行告示,通过前人收集的漏洞编成数据库,根据其扫描对比做出. 具体网站扫描工具有:appscan, ...
- 让WebApi支持Namespace
环境:VS2012 .net 4.0 参考: http://aspnet.codeplex.com/SourceControl/changeset/view/dd207952fa86#Samples/ ...
- Javascript调用WinForm方法
window.external.MyMessageBox('javascript访问C#代码')
- 【Python之路】第二十二篇--Django【基础篇】
1 Django流程介绍 MTV模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业 ...
- jquery slibings选取同级其他元素
jquery选取同级其他元素可以使用slibings方法,end方法可以清除之前的链式操作,相当于重新开始. <script type="text/javascript"&g ...
- 小程序wxParse插件的使用
微信小程序输出html内容数据插件wxParse,可以把带html标签的数据输出为微信小程序正常显示的格式,wxParse插件带有演示,也有使用文档说明. 下载地址:https://github.co ...
- MyBatis 入门(一)
1. MyBatis 概述 MyBatis 是一个半自动化的持久层框架; 核心SQL,开发人员可以进行优化; SQL和Java编码分开,功能边界清晰,一个专注业务,一个专注数据; JDBC: SQL ...
- Part1.1 、RabbitMQ 操作使用
本节目录: 一.最基本的生产者消费者二.acknowledgment 消息不丢失的方法. 三.durable 消息不丢失 (消息持久化) 四.消息获取顺序 RabbitMQ安装. (1.1).ce ...
- begoo——对象的CRUD操作
如果已知主键的值,那么可以使用这些方法进行CRUD操作 对object操作的四个方法Read/Insert/Update/Delete o := orm.NewOrm() user := new(Us ...
- go——切片
切片(slice)可以看作一种对数组的包装形式,它包装的数组为该切片的底层数组.反过来讲,切片是针对其底层数组中某个连续片段的描述,下面的代码声明了一个切片类型的变量: var ips = []str ...