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. 循序渐进VBA EXCEL数据操作小实例

    1 向指定单元格区域内写入数据 Sub example1() ) arr() = Array("A", "B", "C", "D& ...

  2. redis主从复制详述

    一.主从复制详述 原理其实很简单,master启动会生成一个run id,首次同步时会发送给slave,slave同步命令会带上run id以及offset,显然,slave启动(初次,重启)内存中没 ...

  3. VB.Net 经典画圆方法

    计算机图形学课程作业-----画圆 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal ...

  4. 团队-爬取豆瓣电影TOP250-开发环境搭建过程

    从官网下载安装包(http://www.python.org). 安装Python 选择安装路径(我选的默认) 安装Pycharm 1.从官网下载安装包(https://www.jetbrains.c ...

  5. 常见bat(批处理)命令的语法规则

    最近由于在做cocos2d的项目,需要用到一些bat命令,在此做些记录. bat命令用txt文本编辑就行,编辑完之后将后缀名改为bat即可运行.先来一个最简单的例子: @echo off echo \ ...

  6. 走进JDK(八)------AbstractSet

    说完了list,再说说colletion另外一个重要的子集set,set里不允许有重复数据,但是不是无序的.先看下set的整个架构吧: 一.类定义 public abstract class Abst ...

  7. docker相关操作

    docker 安装参照官网一步一步来,特别简单,主要是下载比较慢: docker 需要 管理员权限: docker 相关命令: 容器生命周期管理 — docker [run|start|stop|re ...

  8. Django Model Form

    ModelForm ModelForm结合了Form和Model,将models的field类型映射成forms的field类型,复用了Model和Model验证, 写更少的代码,并且还实现了存储数据 ...

  9. 消息中间件——kafka

    1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下扩展进程间的通信.对 ...

  10. 微信公众号UX分析—— 学生作业小结

    1. 不足: 1. 权威性:个人帐号,显得不够正式. 2. 排版问题: + 没有必要的外接端口,界面设计极度缺少排版.哪怕是个人公众号都不至于如此,更何况这是一个学校的教务平台. 3. 反应不及时或无 ...