LeetCode_18 4Sum
Given an array nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
such that a + b+ c + 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的更多相关文章
- [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: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 ...
- 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 ...
- 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.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 ...
- 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 ...
- 【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 ...
- 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 ...
随机推荐
- 【bzoj4412】[Usaco2016 Feb]Circular Barn
先看成一条链 for一遍找位置 在for一遍算答案 #include<algorithm> #include<iostream> #include<cstring> ...
- 2.eclipse 插件安装烦死人(2)
安装插件的实际结果是:(烦死人),要不是很多插件找不到,要不就是版本不对,要不就是下载了装上没有效果,要不就是在线安装(速度爆慢),好不容易等到结果了,结果是些错…… 最后我的eclipse 3.5. ...
- 步长为float
import numpy as np for i in np.arange(0.005, 0.05, 1): print(i)
- H264--2--语法及结构[5]
名词解释 场和帧 : 视频的一场或一帧可用来产生一个编码图像.在电视中,为减少大面积闪烁现象,把一帧分成两个隔行的场. 片: 每个图象中,若干宏块被排列成片的形式.片分为 ...
- CSU 1807: 最长上升子序列~ 分类讨论
1807: 最长上升子序列~ Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 138 Solved: 17[Submit][Status][Web Bo ...
- Opencv+Zbar二维码识别(二维码校正)
二维码和车牌识别基本都会涉及到图像的校正,主要是形变和倾斜角度的校正,一种二维码的畸变如下图: 这个码用微信扫了一下,识别不出来,但是用Zbar还是可以准确识别的~~. 这里介绍一种二维码校正方法,通 ...
- ZOJ3962 2017 E.Seven Segment Display
数码管从某个状态顺序转移N个状态 计算总共有多少个数码管被点亮 N<=10^9 观察数码管的变化规律,有明显的周期和重复,利用这个性质,计算相对于初始状态,某一位上的某个状态重复了多少次,就可以 ...
- Hadoop伪分布式模式搭建
title: Hadoop伪分布式模式搭建 Quitters never win and winners never quit. 运行环境: Ubuntu18.10-server版镜像:ubuntu- ...
- Linux下磁盘分区、挂载、卸载操作记录
Linux下磁盘分区.挂载.卸载操作记录. 操作环境:CentOS release 6.5 (Final) Last :: from 118.230.194.76 [root@CentOS ~]# [ ...
- 一个完整的mybatis项目,包含增删改查
1.导入jar包,导入相关配置文件,均在自己博客园的文件中 编写mybatis.xml文件 <?xml version="1.0" encoding="UTF-8& ...