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 + ...
随机推荐
- hbase-基础架构
介绍 hbase架构主要由hmaster,zookeeper和regionserver三部分构成,底层数据存储在hdfs中 hmaster 允许有多个master节点,使用zookeeper控制,保证 ...
- DVR NVR
1.NVR: 是(Network Video Recorder即网络硬盘录像机)的缩写.NVR最主要的功能是通过网络接收IPC(网络摄像机)设备传输的数字视频码流,并进行存储.管理,从而实现网络化带来 ...
- 《Linux就该这么学》第二天课程
秦时明月经典语录:很多人被命运安排,而我安排命运.——卫庄 今天介绍了VM 虚拟机的安装以及Linux系统的安装,还讲解了Linux内核 RPM:降低软件的安装难度 源代码+安装规则→将程序源代码与安 ...
- 无法解析的外部命令gethostname
使用gethostname需要连接lib: #include <winsock2.h> #pragma comment(lib, "WS2_32.lib")
- spring中的aop演示
一.步骤(XML配置) 1.导包4+2+2+2 2.准备目标对象 3.准备通知 4.配置进行织入,将通知织入目标对象中 <! -- 3.配置将通知织入目标对象> 5.测试 二.步骤(注解配 ...
- 执行PowerShell脚本的时候出现"在此系 统上禁止运行脚本"错误
使用get-executionpolicy查看当前的脚本执行策略, 默认是Restricted, 也就是不允许任何脚本运行. 此时应该使用set-executionpolicy remotesigne ...
- poj 2505 A multiplication game
题目 题意:两个人轮流玩游戏,Stan先手,数字 p从1开始,Stan乘以一个2-9的数,然后Ollie再乘以一个2-9的数,直到谁先将p乘到p>=n时那个人就赢了,而且轮到某人时,某人必须乘以 ...
- 关于SGA中的granule size
1.什么是granule? granule直译为颗粒,ORACLE为SGA 中的组件(eg:log buffer/db buffer/large pool等)分配的最小单元为一个granule. 所以 ...
- 背水一战 Windows 10 (81) - 全球化
[源码下载] 背水一战 Windows 10 (81) - 全球化 作者:webabcd 介绍背水一战 Windows 10 之 全球化 Demo 格式化数字 示例1.演示全球化的基本应用Locali ...
- 设置Acad2008默认启动 win10设置默认cad2008启动 调试设置.
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\AutoCAD.Drawing.17\shell\open\command]@=&quo ...