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. php Pthread 线程 互斥锁

    在进行并发操作时,会导致共享数据的完整性的问题,要加入锁,在任意时刻只有一个线程访问该对象在PHP中定义专门用于线程同步控制的mutex的函数, pthreads v3 中已经将 Mutex 类移除. ...

  2. windows下mysql安装(zip包方式)

    1.安装地址 https://dev.mysql.com/downloads/mysql/ 2. 解压MySQL压缩包 发现并没有my-default.ini 配置文件主要的作用是设置编码字符集.安装 ...

  3. vuex 中五大核心以及map函数的应用

    什么是vuex? 我理解的vuex就是数据和状态的管理 如果在模块化构建系统中,请确保在开头调用了 Vue.use(Vuex) 五大核心: const store = new Vuex.Store({ ...

  4. C++ 提取网页内容系列之四正则

    标 题: C++ 提取网页内容系列之四作 者: itdef链 接: http://www.cnblogs.com/itdef/p/4173833.html 欢迎转帖 请保持文本完整并注明出处 将网页内 ...

  5. PHP字符串截取函数

    substr函数 描述:实现截取字符串 语法:string substr(string $string,int $start [, int $length ]) 说明:如果省略length,则返回从s ...

  6. crontab定时时间解释

    用户所建立的crontab文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下: minute hour day mo ...

  7. spring boot开发笔记——mybatis

    概述   mybatis框架的优点,就不用多说了,今天这边干货主要讲mybatis的逆向工程,以及springboot的集成技巧,和分页的使用   因为在日常的开发中,当碰到特殊需求之类会手动写一下s ...

  8. Flask 中的路由系统

    基本用法 Django的路由系统url集中在一起,而Flask的路由系统以装饰器的形式装饰在视图上如: @app.route("/",methods=["GET" ...

  9. PHP基础架构

    PHP基础架构 一.PHP简介 PHP是一种非常流行的高级脚本语言,尤其适合Web开发,快速.灵活和实用是PHP最重要的特点.PHP自1995年由Lerdorf创建以来,在全球得到了非常广泛的应用. ...

  10. tk.mybatis通用工具采坑记

    tk.mybatis通用工具pom <!--mybatis依赖--> <dependency> <groupId>org.mybatis.spring.boot&l ...