【leetcode列】3Sum
现在的问题是,我开始思考:一是制定了一些,然后设置这个数字,除了里面找到两个数字。最后,计算和。重复,供N的数量,需要N-2周期。
我的问题是如何找到的其他两个数字,其实,我想引用Two Sum内部解决方案,它是用Hashtable存。纽带值是<随意两个数的和,<下标1,下标2>>,可是构造这个Hashtable就须要O(N^2),后面真正解的时候有须要O(N^2)。
參考了大牛的解法后,明确了找两个数还是用两个下标同一时候往中间移动比較好,以下上代码。
import java.util.ArrayList;
import java.util.Arrays; public class Solution {
public static ArrayList<ArrayList<Integer>> threeSum(int[] numbers) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (numbers.length < 3) {
return result;
}
Arrays.sort(numbers);
for (int i : numbers) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < numbers.length - 2; i++) {
int lp = i + 1;
int rp = numbers.length - 1;
while (lp < rp) {
int sum = numbers[i] + numbers[lp] + numbers[rp];
if (sum == 0) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp.add(numbers[i]);
tmp.add(numbers[lp]);
tmp.add(numbers[rp]);
result.add(tmp);
do {
lp++;
} while (lp < rp && numbers[lp] == numbers[lp + 1]);
do {
rp--;
} while (lp < rp && numbers[rp] == numbers[rp - 1]);
} else if (sum < 0) {
lp++;
} else if (sum > 0) {
rp--;
}
}
}
return result;
} public static void main(String[] args) {
int[] a = { -1, 0, 1, 2, -1, -4 };
ArrayList<ArrayList<Integer>> result = threeSum(a);
for (ArrayList<Integer> item : result) {
for (Integer i : item) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
【leetcode列】3Sum的更多相关文章
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- [Leetcode][015] 3Sum (Java)
题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过 ...
- LeetCode 259. 3Sum Smaller (三数之和较小值) $
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
随机推荐
- 在浏览器中通过bartender,调用条码打印机的active控件代码的实现
系统中须要在浏览器,直接调用条码打印机,打印出产品条码. 现实中的条码打印机,品种繁多,非常难在一个程序中实现, 于是我们用已经支持全部条码打印机的bartender软件 调用它的api ,来实如今浏 ...
- boost库在工作(33)网络服务端之三
在这个例子里,表示服务器与一个客户端的沟通渠道,就是一个连接,封装为类CConnect.它是当服务器接收到一个客户端连接请求之后创建的,主要用来就是管理这个连接的生命周期,以及数据的接收和发送.从生命 ...
- 看来IT技术与军事技术都是相通的——都是对新事物极为敏感的领域
这是读到这段时候的感想: 和海军中那些狂热的相信“皇军不可战胜”的大舰巨炮主义者们不同,山口对于与美国开战的主张是持坚定的反对态度的,和山本五十六都做过日本驻美武官的山口都认为一旦与美开战,日本或许能 ...
- [Android学习笔记]继承自ViewGroup的控件的过程学习
ViewGroup文档 http://developer.android.com/training/index.html 继承自ViewGroup需要重写onLayout方法用来为子View设定位置信 ...
- 【机器学习】SVM核函数
知识预备 1. 回顾:logistic回归出发,引出了SVM,即支持向量机[续]. 2. Mercer定理:如果函数K是上的映射(也就是从两个n维向量映射到实数域).那么如果K是一个有效核函数(也称 ...
- 智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用 先上图: 现在的智能控制都是基于微控制器,随着智能的手持终端的普及,基于智能终端的控制就会越来越普遍. WIFI便是其中的一 ...
- learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange
learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange Haskell AND Lisp vs. Ha ...
- Tutorial: 结合使用AngularJS和Django
好吧,我承认自己很懒,时间又不够用. 翻译的几个文章都是虎头蛇尾,但我保证这次肯定不太监. 关键的单词不翻译,实在觉得翻译成汉语很别扭,括号里是参考翻译. 有问题和建议尽管提出来,我会改进完善. Tu ...
- hdu1054(最小顶点覆盖)
传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...
- SSDTHook实例--编写稳定的Hook过滤函数
解说怎样写Hook过滤函数,比方NewZwOpenProcess.打开进程. 非常多游戏保护都会对这个函数进行Hook. 因为我们没有游戏保护的代码,无法得知游戏公司是怎样编写这个过滤函数. 我看到非 ...