leetcode — 4sum
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的更多相关文章
- LeetCode——4Sum & 总结
LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...
- [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 ...
- [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 ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- 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 ...
- 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 ...
- leetcode 4sum python
class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...
- 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 = ...
- 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 + ...
随机推荐
- PostFix添加多个端口
/usr/local/*/config/postfix/master.cf 在/etc/services中搜索smtp,复制,添加smtp2<与上面一步对应起来的服务名>,后面为需要添加的 ...
- 移动端web兼容各种分辨率写法
移动端web开发最好用rem单位,再设置以下js,以iphone6 750*1334为基准 <script> var size = document.documentElement.cli ...
- CQOI2018 简要题解
破解D-H协议 列个式子会发现是BSGSBSGSBSGS的模板题,直接码就是了. 代码: #include<bits/stdc++.h> #include<tr1/unordered ...
- s4 docker 网络2进阶
多容器复杂应用的部署 基于 flask容器,链接外部另一个redis 容器 docker pull redis sudo docker run -d --name redis redis # redi ...
- 第38章:MongoDB-集群--Replica Sets(副本集)---多机的搭建
①机器环境 182.48.115.236 master-node(主节点) 182.48.115.237 slave-node1(从节点) 182.48.115.238 slave- ...
- kubernetes中filebeat以sidecar方式和应用一起部署,并且传入环境变量
本文的环境介绍 [root@m-30-1 ~]# kubectl version Client Version: version.Info{Major:"1", Minor:&qu ...
- 【pycharm 密钥】pycharm 2017 密钥
server选项里边输入: http://idea.liyang.io 亲测可用!!!
- httpd-vhosts.conf配置
<VirtualHost 127.0.0.1:80> ServerName zjm.appems.com DocumentRoot E:\qian\web <Directory &q ...
- 595. Big Countries --- SQL related from leetcode
595. Big Countries There is a table World +-----------------+------------+------------+------------- ...
- 进程控制(Note for apue and csapp)
1. Introduction We now turn to the process control provided by the UNIX System. This includes the cr ...