import java.util.Arrays;
import java.util.HashSet;
import java.util.Set; /**
* Source : https://oj.leetcode.com/problems/4sum/
*
* Created by lverpeng on 2017/7/10.
*
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
* Find all unique quadruplets in the array which gives the sum of target.
*
* Note:
*
* Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
* The solution set must not contain duplicate quadruplets.
*
* For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
*
* A solution set is:
* (-1, 0, 0, 1)
* (-2, -1, 1, 2)
* (-2, 0, 0, 2)
*/
public class FourSum { /**
*
* 分解为多个3sum问题,求出3sum问题普通解法
* 3sum的解法就是分为多个2sum问题,2sum问题就可以使用原来的方法进行解答
*/
public Set<Integer[]> threeSum (int[] num, int total) {
Set<Integer[]> set = new HashSet<Integer[]>();
// Arrays.sort(num);
for (int i= 0; i < num.length; i++) {
int left = i + 1;
int right = num.length - 1;
int sum2 = total - num[i];
while (left < right) {
if (num[left] + num[right] == sum2) {
Integer[] answer = {num[i], num[left], num[right]};
set.add(answer);
left++;
right--;
while (left < right && num[left] + num[right] == sum2) {
Integer[] ans = {num[i], num[left], num[right]};
set.add(ans);
left++;
right--;
} } else if (num[left] + num[right] < sum2) {
left++;
while (left < right && num[left] + num[right] < sum2) {
left++;
}
} else {
right--;
while (left < right && num[left] + num[right] > sum2) {
right--;
}
}
}
}
return set;
} /**
* 对数组排序,分解为3sum处理
*
* @param num
* @param total
* @return
*/
public Set<Integer[]> fourSum (int[] num, int total) {
Set<Integer[]> set = new HashSet<Integer[]>();
Arrays.sort(num);
int[] subArr = new int[num.length - 1];
for (int i = 0; i < num.length; i++) {
int a = num[i];
System.arraycopy(num, i + 1, subArr, 0, num.length - i - 1);
Set<Integer[]> threeSumResult = threeSum(subArr, total - a);
for (Integer[] intArr : threeSumResult) {
Integer[] arr = new Integer[4];
arr[0] = a;
System.arraycopy(intArr, 0, arr, 1, 3);
set.add(arr);
}
}
return set;
} public void printList (Set<Integer[]> set) {
for (Integer[] arr : set) {
System.out.println(Arrays.toString(arr));
}
} public static void main(String[] args) {
FourSum fourSum = new FourSum();
int[] arr = {1, 0, -1, 0, -2, 2};
Set<Integer[]> set = fourSum.fourSum(arr, 0);
fourSum.printList(set);
} }

leetcode — 4sum的更多相关文章

  1. LeetCode——4Sum &amp; 总结

    LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...

  2. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  3. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

  5. Leetcode 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  6. Leetcode: 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. leetcode 4sum python

    class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...

  8. LeetCode 4Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  9. LeetCode——4Sum

    1. Question Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + ...

随机推荐

  1. 洛谷4556 [Vani有约会]雨天的尾巴

    原题链接 每个点开一个权值线段树,然后用树上差分的方法修改,最后自底向上暴力线段树合并即可. 不过空间较大,会\(MLE\),写个内存池就可以了. #include<cstdio> #in ...

  2. Python 多进程编程之 进程间的通信(Queue)

    Python 多进程编程之 进程间的通信(Queue) 1,进程间通信Process有时是需要通信的,操作系统提供了很多机制来实现进程之间的通信,而Queue就是其中的一个方法----这是操作系统开辟 ...

  3. ubuntu16.04 下安装 visual studio code 以及利用 g++ 运行 c++程序

    参考链接:1. http://www.linuxidc.com/Linux/2016-07/132798.htm(安装vs code) 2.https://blog.csdn.net/qq_28598 ...

  4. 《Linux就该这么学》第六天课程

    每个人都有梦想,同时也有理想,当一个人的梦想与理想相同时会发生什么? 搜集了一些对新手有用的表格 原创地址:https://www.linuxprobe.com/chapter-05.html use ...

  5. Ubuntu Apache 不同端口监听不同站点

    在/etc/apache2/apache2.conf 中,把项目根目录设置成默认的/var/www 不要设置在某个站点的路径下(我就是配置第一个站点时改了这里才会配置第二个站点时好久弄不出来) 在 / ...

  6. Oracle存储过程,游标使用

    Oracle存储过程: 语法: CREATE [OR REPLACE] PROCEDURE procedure_name (arg1 [mode1] datatype1,arg2 [mode2] da ...

  7. ubuntu 配置jdk报错解决办法

    vi /etc/profile ,添加如下代码 export JAVA_HOME=/home/mark/android/jdk1.8 export JRE_HOME=/home/mark/androi ...

  8. jvm虚拟机--垃圾回收子系统

    转载自cyc2018的github:https://github.com/CyC2018/Interview-Notebook/blob/master/notes/Java%20%E8%99%9A%E ...

  9. 进度条(progress_bar)

    环境:linux.centos6.5 #include<stdio.h> #include<unistd.h> int main() { ]={'\0'}; char ch[] ...

  10. Visual Studio 2017中使用正则修改部分内容

    最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效果如下 修改前: /// <summary> /// ...