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.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
代码如下:算法复杂度O(n^2)
class Solution {
public List<List<Integer>> threeSum(int[] num) {
ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
if (num == null || num.length < 3)
return result;
Arrays.sort(num);
int lastNum = num[0] - 1;
for (int i = 0; i < num.length - 2 && num[i] <= 0; i++) {
if (num[i] != lastNum) {
result.addAll(find(num, i));
lastNum = num[i];
}
}
return result;
}
private ArrayList<List<Integer>> find(int[] array, int index) {
int i = index + 1;
int j = array.length - 1;
int lastI = array[index] - 1;
int lastJ = array[index] - 1;
ArrayList<List<Integer>> Lists = new ArrayList<List<Integer>>();
while (i < j) {
if (array[i] + array[j] + array[index] == 0) {
if (array[i] == lastI) {
i++;
} else if (array[j] == lastJ) {
j--;
} else {
lastI = array[i];
lastJ = array[j];
ArrayList<Integer> curList = new ArrayList<Integer>();
curList.add(array[i]);
curList.add(array[j]);
curList.add(array[index]);
Lists.add(curList);
i++;
j--;
}
} else if (array[i] + array[j] + array[index] > 0) {
j--;
} else {
i++;
}
}
return Lists;
}
}
注意几点:
1,如果遇到重复的数字,那么可以直接跳过。外层循环和内层循环都可以,因为都是排过序的,如果遇到同样的数字必然产生同样的结果。
2,具体实现的时候直接让lastNum等于开始的数字减一,保证第一个数字不会被跳过。
算法的核心思路是,首先排序。这个算法复杂度是O(n*logn)。接下来先依次找每一个数字,每找到一个数字,剩下的目标就是在剩下的数字中找到2个数,使他们之和为0。正因为排过序,所以找两个数字的过程算法复杂度可以下降到O(n)。也就是从两头往中间。每次如果和大于0,就说明在外层数字确定的情况下,这两个数字的和大了,也就要让其变小,让j--,反之则i++。
3sum问题的解决的更多相关文章
- 《LeetBook》leetcode题解(18) : 4Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [Locked] 3Sum Smaller
3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...
- 16 3Sum Closest(输出距离target最近的三个数的和Medium)
题目意思:给一个数组,给一个target,找三个数的和,这个和要与target距离最近,输出这个和 思路:这个题比3sum要稍微简单一点,如果需要优化,也可以去重,不过因为结果唯一,我没有去重. mi ...
- 3Sum探讨(Java)
探讨一下leetcode上的3Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
随机推荐
- CodeMirror简介
Javascript由于其作为Web标准的独特地位,很多人甚至希望它能一统前后端开发. Javascript的本质工作首先肯定的Web前端开发,本文主要想介绍的CodeMirror是一款Web Edi ...
- DSP的Gel作用
转自:http://blog.csdn.net/azhgul/article/details/6660960 最近刚在研究Davinci系,特此MARK下,以资后续学习之用. DSP的Gel作用 1 ...
- 利用H5和ChromiumWebBrowser构建应用
chromium是google chrome浏览器所采用的内核,最开始由苹果的webkit发展而出,由于webkit在发展上存在分歧,而google希望在开发上有更大的自由度,2013年google决 ...
- Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】
2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...
- ES6笔记(一):ES6所改良的javascript“缺陷”
块级作用域 ES5没有块级作用域,只有全局作用域和函数作用域,由于这一点,变量的作用域甚广,所以一进入函数就要马上将它创建出来.这就造成了所谓的变量提升. ES5的"变量提升"这一 ...
- Dynamic CRM 查询实体记录 被共享给了 哪个用户
--客户表名"new_customer" SELECT u.FullName AS 被共享人,a.new_name AS 客户名称,sup.SystemUserid AS 共享人I ...
- iOS空心圆下载进度指示器控件
self.layer = [CAShapeLayer layer]; self.layer.frame = CGRectMake(, , , ); self.layer.position = self ...
- IOS 杂笔-11(实现在外部无法改变UIView的size)
我想题目说的或许不是很清楚,那么现在我详细介绍一下这篇随笔内容. 在外部无法改变UIVIew控件的size. 这里说是UIView,但是事实上,是大多数控件而绝非仅UIView. 想要实现在外部无法改 ...
- 读<<领域驱动设计-软件核心复杂性应对之道>>有感
道可道,非常道. 名可名,非常名. 无名天地之始,有名万物之母. ---老子 关于标题 好久没写东西了,动笔的动机是看完了一本书,想写点总结性的东西,一是为了回顾一下梳理知识点,二是为了日后遗忘时能有 ...
- Linux下,拷贝文件时,排除某些文件
一下是自己用到到几次实践,觉得很赞: 1.拷贝文件时,排除某些不需要的文件: 1)使用xargs来做: ls /tmp/test/ |grep -v .gz |xargs -i cp -r ...