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 ...
随机推荐
- c++ builder 2010 错误 F1004 Internal compiler error at 0x9740d99 with base 0x9
今天遇到一个奇怪的问题,拷贝项目后,在修改,会出现F1004 Internal compiler error at 0x9740d99 with base 0x9 ,不管怎么改,删除改动,都没用,关闭 ...
- redis incr incrby decr decrby命令
incr.incrby.decr.decrby命令的作用和用法 redis中incr.incrby.decr.decrby属于string数据结构,它们是原子性递增或递减操作. incr递增1并返回递 ...
- Windows安装RabbitMQ集群的几个注意点
记录一下RabbitMQ在windows平台下安装的几个注意点- -,好记性不如烂笔头 安装过程与Linux安装一致,教程参照官网集群配置:此处只列举出几个注意点: 1. erlang的版本需要一致, ...
- 设计模式03备忘录(java)
先贴代码有空来写内容. 备忘录1 //简单的备忘录,只可以记录上一次修改前的状态,实现撤回一次的操作. class Student{ private String name; private Stri ...
- 从网上找的 visual studio 的各个版本下载地址,vs2010/vs2012/vs2013带注册码
从网上找的 visual studio 的各个版本下载地址,很全,从 6.0 一直 到 vs2013,要的拿去吧... Microsoft Visual Studio 6.0 下载:英文版360云盘下 ...
- 兼容当前多浏览器的渐变颜色背景gradient的写法
经常有一些时候需要使用渐变背景,使用长条图片有点太不高大上了,于是自己写了个小例子,兼容多浏览器就要为每一个浏览器写对应的CSS,太低版本的浏览器只能使用图片做背景. 下面是当前五大浏览器对gradi ...
- cmd命令汇总
一 cmd 命令 cmd命令大全(第一部分) winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr------ ...
- javascript操作系统检测
function detectOS() { var sUserAgent = navigator.userAgent;console.log(sUserAgent); var isWin = (nav ...
- 集成shareSDK错误总结(新浪微博)
错误1. . 以上错误是由于没有添加-ObjC的原因,在targets->Build Setting ->Other Linker Flags中添加-ObjC 添加方法如下 错误2 授权回 ...
- 让你少走弯路的搭建树莓派的Net与NodeJS运行环境
树莓派是当前最火的嵌入计算平台没有之一,树莓派可以给我们无数的想象,树莓派的高性能.低功耗.低成本.可扩展性(最新的树莓派原生支持WIFI和蓝牙,这功能太赞了)深受大家的喜爱.虽然树莓派到目前为止 ...