现在的问题是,我开始思考:一是制定了一些,然后设置这个数字,除了里面找到两个数字。最后,计算和。重复,供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的更多相关文章

  1. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  2. [Leetcode][016] 3Sum Closest (Java)

    题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...

  3. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  4. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  5. [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 < ...

  6. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

  7. [Leetcode][015] 3Sum (Java)

    题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过 ...

  8. 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 < ...

  9. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

随机推荐

  1. MIPS平台目标机安装Oprofile时出现error

    在Debian下安装Oprofile 重要:应该使linux内核选项支持 在 .config 文件中设置CONFIG_PROFILING=y和CONFIG_OPROFILE=y. 重新编译,烧写.使用 ...

  2. 从Hadoop骨架MapReduce在海量数据处理模式(包括淘宝技术架构)

    从hadoop框架与MapReduce模式中谈海量数据处理 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显兴奋,认为它们非常是神奇.而神奇的东西常能勾 ...

  3. thinkphp 3.2.3 入门示例

    原文:thinkphp3.2 1.安装WAMPServer,到D:\wamp\. 2.下载ThinkPHP3.2.3核心版.解压缩后,放到D:\wamp\www\MyWeb\.打开浏览器,输入网址:h ...

  4. [Android]图片资源管理学习

    一.概念 几个概念:1.像素:像素是组成图像的最基本单位:点该点自身有大小,其中保存了颜色值 2.屏幕尺寸:screen size手机屏幕的物理尺寸.单位:inch(英寸)ex:4.0英寸 3.8英寸 ...

  5. 【PostgreSQL】PostgreSQL语法

    在阅读的过程中有不论什么问题.欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 一.PostgreSQL时间类型转换 --时间类型转成字符类型 select t ...

  6. IOS7最新的系统漏洞

    苹果近期就实用户发现了一个新iOS7系统漏洞,利用这个漏洞绕过password输入界面,在不知道password的情况下打开你近期使用的软件. 而这个过程仅仅需短短的5秒钟! 经測试,这个漏洞并没那么 ...

  7. 如何高效地向Redis插入大量的数据(转)

    最近有个哥们在群里问,有一个日志,里面存的是IP地址(一行一个),如何将这些IP快速导入到Redis中. 我刚开始的建议是Shell+redis客户端. 今天,查看Redis官档,发现文档的首页部分( ...

  8. JAVA进阶----ThreadPoolExecutor机制(转)

    ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...

  9. ipsec vpn私网数据大量掉包问题

    周四出现了一个很奇葩的问题,所有的站点的VPN通信都是正常的,唯独郑州节点和中心节点的私网数据长ping掉包量达到20%左右,在中心节点ping郑州节点公网IP没有发现掉包问题,故障排除如下: 1.测 ...

  10. [置顶] android系统功能调用大全

    1.从google搜索内容  Intent intent = new Intent();  intent.setAction(Intent.ACTION_WEB_SEARCH);  intent.pu ...