【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个数来,使得这三个数的 ...
随机推荐
- 更好的自动ssh登录
更好的自动ssh登录 解决~/.ssh/known_hosts 过期问题. bash + expect bash:ssh.sh #!/bin/bash help(){ echo "usage ...
- Android 进行单元測试难在哪-part3
原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...
- [Android]Eclipse的使用
1.取消Eclipse拼写检查 General -> Editors -> Text Editors -> Spelling 取消enable spell checking 前面的勾 ...
- [C++]Hello C++
最先进项目中需要用到C++做开发,所以开始学习C++,典型的眼高手低,刚开始觉得还算上手,之后越学越觉得复杂. 相比C#,C++确实需要开发者投入更多的精力去设计与维护. 以下是最近对C++开发的一些 ...
- java基础---->摘要算法的介绍 (转)
数据摘要算法是密码学算法中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数据签名.数据完整性校验等功能,由于其不可逆性,有时候会被用做敏感信息的加密.数据摘要算法也被称为哈希(Hash)算法. ...
- UVALive 5790 Ball Stacking 解题报告
比赛总结 题目 题意: 有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取.求选的球最大的权值和. 题解: 将这堆球转成举行,第一行是(0,0),第二个是(1,0) ...
- Cordova/Phonegap 升级至 2.8.1
相关链接 Apache Cordova 项目首页: http://cordova.apache.org/ Apache Cordova 历史版本列表: http://archive.apache.or ...
- Java 泛型具体解释
在Java SE1.5中.添加了一个新的特性:泛型(日本语中的总称型).何谓泛型呢?通俗的说.就是泛泛的指定对象所操作的类型.而不像常规方式一样使用某种固定的类型去指定. 泛型的本质就是将所操作的数据 ...
- SUSE Linux 报错:too many open files in system
现网执行的oracle数据库,有一天突然报错(alert日志):too many open files in system,须要对操作系统同意句柄数进行扩充,查阅了非常多资料,改动点主要集中在例 ...
- Nginx特点
Nginx特点:1,跨平台:Nginx 能够在大多数 Unix like OS编译执行,并且也有Windows的移植版本号.2,配置异常简单:很easy上手.配置风格跟程序开发一样,神一般的配置.3, ...