leetcode — 3sum
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的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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 ...
- [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 ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode 3Sum Closet
二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...
随机推荐
- R 语言安装
在linux下,对于手动安装的软件,当时间长了,我们就会忘记安装这个软件的细节.这就不利于以后软件的卸载工作了.而yum则会帮我们记住相关安装细节,当软件被卸载的时候,没用的文件也会一并被删除.因此, ...
- unbuntu 安装 teamviewer
下载 teamviewer 安装包 使用 dpkg 安装 deb 安装包 使用 sudo apt-get install -f 解决依赖问题
- MyOD
一.实现目的: 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 二.功能简介 1.Linux下的od功能是将指定文件内容以八进制.十进制.十六进 ...
- 20165213Java第二次实验
实验二Java面向对象程序设计 实验1 实验目的与要求: 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交 ...
- MD5盐值加密
加密思路 思路解析:(数据解析过程基于16进制来处理的,加密后为16进制字符串) 加密阶段: 对一个字符串进行MD5加密,我们需要使用到MessageDigest(消息摘要对象),需要一个盐值(sal ...
- Notes : <Hands-on ML with Sklearn & TF> Chapter 1
<Hands-on ML with Sklearn & TF> Chapter 1 what is ml from experience E with respect to som ...
- tarjan求强连通分量+缩点+割点/割桥(点双/边双)以及一些证明
“tarjan陪伴强联通分量 生成树完成后思路才闪光 欧拉跑过的七桥古塘 让你 心驰神往”----<膜你抄> 自从听完这首歌,我就对tarjan开始心驰神往了,不过由于之前水平不足,一 ...
- 几种String对象方法的区别
1.在String对象方法中,发现.slice()方法和.substring()方法的作用几乎相同,都是根据起始索引返回截取得到的字符串.经过查阅资料和实测得到区别: 正常情况下索引都为正值,返回值为 ...
- Android-Java-构造函数间调用&this内存图
构造函数间调用: 描述Person对象: package android.java.oop08; // 描述Person对象 public class Person { public String n ...
- 还原Azure DevOps Server (TFS)中误删除的生成流水线
流水线历史记录 DevOps Server流水线的历史记录有完善的版本日志,用户可以随时回退到修改过程中的任何一个版本,还能比较差异.这个历史记录功能可以和代码库中的版本控制媲美. 图一:生成历史记录 ...