import java.util.*;

/**
* Source : https://oj.leetcode.com/problems/3sum/
*
* Created by lverpeng on 2017/7/10.
*
* 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.
*
* Note:
*
* Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
* The solution set must not contain duplicate triplets.
*
* For example, given array S = {-1 0 1 2 -1 -4},
*
* A solution set is:
* (-1, 0, 1)
* (-1, -1, 2)
*/
public class SumEqualsZero { /**
* 最简单的方法,计算出所有三个数和为0的情况
*
* @param s
* @return
*/
public Set<Integer[]> findThreeNum (int[] s) {
Arrays.sort(s);
System.out.println(Arrays.toString(s));
Set<Integer[]> result = new HashSet<Integer[]>();
if (s.length < 4) {
return null;
}
for (int i = 0; i < s.length - 2; i++) {
for (int j = i + 1; j < s.length - 1; j++) {
for (int k = j + 1; k < s.length; k++) {
if(s[i] + s[j] + s[k] == 0) {
Integer[] arr = {s[i], s[j], s[k]};
result.add(arr);
}
}
}
} return result;
} /**
* 可以转化为和twosum一样的问题,相当于是多个twosum问题
* a + b = -c
* 就是两个数的和是一个定值,针对每一种c的情况求出a、b
*
* @param s
* @return
*/
public Set<Integer[]> findThreeNum1 (int[] s) {
Arrays.sort(s);
Set<Integer[]> set = new HashSet<Integer[]>();
for (int i = 0; i < s.length - 2; i++) {
int total = -s[i];
int left = i + 1;
int right = s.length -1;
while (left < right) {
if (s[left] + s[right] == total) {
Integer[] arr = {s[i], s[left], s[right]};
set.add(arr);
left ++;
right --;
} else if (s[left] + s[right] > total) {
while (left < right && s[left] + s[right] > total) {
right --;
}
} else {
while (left < right && s[left] + s[right] < total) {
left ++;
}
}
}
}
return set;
} public static void main(String[] args) {
SumEqualsZero sumEqualsZero = new SumEqualsZero();
int[] arr = {-1, 0 ,1, 2, -1, -4};
printList(sumEqualsZero.findThreeNum(arr));
printList(sumEqualsZero.findThreeNum1(arr));
} public static void printList (Set<Integer[]> list) {
for (Integer[] i : list) {
System.out.println(Arrays.toString(i));
}
} }

leetcode — 3sum的更多相关文章

  1. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

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

  3. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  4. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  5. LeetCode: 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

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

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

  8. leetcode—3sum

    1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  9. Leetcode 3Sum Closet

    二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...

随机推荐

  1. JavaScript日历(es5版本)

    近期在知乎上看到这么一个帖子,题主说自己JavaScript都学完了,结果老师留的作业还是不会写,就是写一个日历的插件,结果楼下一堆大牛出现了,百度的阿里的纷纷站出来发表自己的看法,有人认为简单,有人 ...

  2. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

  3. Struts2学习第四天——拦截器及文件上传

    1.概述 Struts2的很多核心功能都是由拦截器完成的. 拦截器很好的实现了AOP的编程思想,在动作的执行之前和结果的返回之后,做拦截处理. 2.struts2的默认拦截器栈 3.自定义拦截器 St ...

  4. 统计C/C++代码行数

    近日在写一个统计项目中C/C++文件(后缀名:C/CPP/CC/H/HPP文件)代码行数的小程序.给定包含C/C++代码的目录,统计目录里所有C/C++文件的总代码行数.有效代码行数.注释行数.空白行 ...

  5. ng 的 ng-repeat(对象) 把对象的 key 和value 都拿出来 循环

    对象的repeat  是这种形式

  6. 完善版封装canvas分享组件

    import regeneratorRuntime from "../../../lib/regenerator-runtime/runtime"; let ctx = false ...

  7. 8:String类

    String类 String类的特点: 字符串对象一旦被初始化就不会被改变. 字符串是最常用的类型之一,所以为了使用方便java就给封装成了对象方便使用  public static void str ...

  8. Markdown新手教程

    目录 什么是Markdown? 用Markdown写作有什么优缺点? 有哪些比较好的Markdown写作工具? markdown语法 标题 水平分区线 引用 中划线 斜体 粗体 斜粗体 链接 图片 无 ...

  9. C++随机数引擎

    C++的随机数引擎有以下几个要点需要注意:  1.随机数发生器使用同一种子会生成相同序列的随机数序列 2.为了让程序每次运行都会生成不同的随机结果,我们可以使用 time(0) 产生一个随机数种子 3 ...

  10. 使用Qt开发绘制多个设备的流量曲线图(附带项目图)

    一.说明: 在实际项目中,主要是使用Qt开发CS程序,当然主要是客户端.公司项目中有这个需求是实时显示多个设备的流量曲线图,设备将流量信息发给服务端,服务端再将信息通过Socket发给Qt客户端,Qt ...