Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + bc + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [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 List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i != 0 && nums[i] == nums[i - 1])
continue;
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1])
continue;
int k = j + 1;
int l = nums.length - 1;
while (k < l) {
int sum = nums[i] + nums[j] + nums[k] + nums[l];
if (sum == target) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[k]);
list.add(nums[l]);
res.add(list);
k++;
l--;
// 去重复
while (k < l && nums[k] == nums[k - 1]) {
k++;
}
while (k < l && nums[l] == nums[l + 1]) {
l--;
}
} else if (sum < target) {
k++;
} else {
l--;
}
}
}
}
return res;
}
 public List<List<Integer>> fourSum3(int[] num, int target) {
ArrayList<List<Integer>> ans = new ArrayList<>();
if (num.length < 4)
return ans;
Arrays.sort(num);
for (int i = 0; i < num.length - 3; i++) {
if (num[i] + num[i + 1] + num[i + 2] + num[i + 3] > target)
break; // first candidate too large, search finished
if (num[i] + num[num.length - 1] + num[num.length - 2] + num[num.length - 3] < target)
continue; // first candidate too small
if (i > 0 && num[i] == num[i - 1])
continue; // prevents duplicate result in ans list
for (int j = i + 1; j < num.length - 2; j++) {
if (num[i] + num[j] + num[j + 1] + num[j + 2] > target)
break; // second candidate too large
if (num[i] + num[j] + num[num.length - 1] + num[num.length - 2] < target)
continue; // second candidate too small
if (j > i + 1 && num[j] == num[j - 1])
continue; // prevents duplicate results in ans list
int low = j + 1, high = num.length - 1;
while (low < high) {
int sum = num[i] + num[j] + num[low] + num[high];
if (sum == target) {
ans.add(Arrays.asList(num[i], num[j], num[low], num[high]));
while (low < high && num[low] == num[low + 1])
low++; // skipping over duplicate on low
while (low < high && num[high] == num[high - 1])
high--; // skipping over duplicate on high
low++;
high--;
}
// move window
else if (sum < target)
low++;
else
high--;
}
}
}
return ans;
}

LeetCode_18 4Sum的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

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

  5. No.018:4Sum

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

  6. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  7. 3Sum & 4Sum

    3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...

  8. 【leetcode】4Sum

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

  9. 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum

    2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...

随机推荐

  1. Python extensions for Windows

    Python extensions for Windows pywin32 214 Python extensions for Windows Maintainer: Mark Hammond Hom ...

  2. 容器HashSet原理(学习)

    一.概述 使用HashMap存储,非线程安全: 二.实现 HashSet 底层使用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,相关 HashSet 的操作,基本上都是直接调 ...

  3. HttpClient-01基本概念

    Http 协议应该是互联网中最重要的协议.持续增长的 web 服务.可联网的家用电器等都在继承并拓 展着 Http 协议,向着浏览器之外的方向发展. 虽然 jdk 中的 java.net 包中提供了一 ...

  4. 【Silverlight】Bing Maps学习系列(七):使用Bing Maps的图片系统(Tile System)

    [Silverlight]Bing Maps学习系列(七):使用Bing Maps的图片系统(Tile System) 目前包括微软必应地图在内的几乎所有在线电子地图(如:Google Maps等)都 ...

  5. CSU 1807: 最长上升子序列~ 分类讨论

    1807: 最长上升子序列~ Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 17[Submit][Status][Web Bo ...

  6. Java 泛型 一

    泛型在Java中有很重要的地位,网上很多文章罗列各种理论,不便于理解,本篇将立足于代码介绍.总结了关于泛型的知识. 先看下面的代码: List list = new ArrayList(); list ...

  7. 在Android.mk文件中输出打印消息 (转载)

    转自:http://blog.csdn.net/xiaibiancheng/article/details/8479694 在进行Android NDK的开发当中有时想看看Android.mk文件当中 ...

  8. Linux 进程间通讯方式 pipe()函数 (转载)

    转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...

  9. PCB 批量Word转PDF实现方法

    自上次公司电脑中毒带来的影响,导致系统自动生成的Word档PCB出货报告,通过公司邮件服务器以附件的方式发送给客户后,客户是无法打开或打开缓慢的现象,如果将Word档转为PDF后在客户端是可以正常打开 ...

  10. E20180119

    Foundation   n. 基础; 地基; 粉底; 基金(会); hybrid   n. 杂种; 杂交生成的生物体; 混合物; 混合词; adj. 混合的; 杂种的;