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]
]
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& a, int target) {
vector<vector<int>> res;
const int n = a.size();
if (n < ) return res;
std::sort(a.begin(),a.end());
for (int i = ; i < n-; ++i) {
if(i!= && a[i]==a[i-]) {continue;} //去重
for (int j = i+; j < n-; ++j) {
if(j!=i+ && a[j]==a[j-]){continue;} //去重
int low = j+;
int high = n-;
while (low < high) {
int sum = a[i]+a[j]+a[low]+a[high];
if (sum > target) {
high--;
} else if (sum < target) {
low++;
} else {
vector<int> tep = {a[i],a[j],a[low],a[high]};
res.emplace_back(tep);
while(low <high && a[low]==a[low+]) {low++;}//去重
while(low <high && a[high]==a[high-]) {high--;}//去重
low++;
high--;
}
}
}
}
return res;
}
};
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new LinkedList<>();
if (nums.length<4) return res;
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 lo = j+1,hi = nums.length-1;
while(lo<hi){
int sum = nums[i]+nums[j]+nums[lo]+nums[hi];
if(sum==target){
res.add(Arrays.asList(nums[i],nums[j],nums[lo],nums[hi]));
//答案去重
while(lo<hi&&nums[lo]==nums[lo+1]) lo++;
while(lo<hi&&nums[hi]==nums[hi-1]) hi--;
lo++;
hi--;
}
else if(sum<target) lo++;
else hi--;
}
}
}
return res;
}
}
18. 4Sum(双指针)的更多相关文章
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 1. Two Sum&&15. 3Sum&&18. 4Sum
题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- 【LeetCode】18. 4Sum (2 solutions)
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- [LeetCode] 18. 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 ...
- 18. 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 ...
随机推荐
- [archlinux][crypto] 从T450迁移archlinux操作系统至T460s笔记本
从T450笔记本迁移archlinux操作系统之T460s笔记本,同时: 1. 使用cryptsetup做底层块加密. 2. 全新使用btrfs文件系统. 一,硬盘分区. 1T的SSD,使用U ...
- 20165336 实验二 Java面向对象程序设计
20165336 实验二 Java面向对象程序设计 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:康志强 学号:20165336 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...
- js禁用页面上右键菜单、选中和复制
有时候我们不想页面上的内容被人复制走,那么就可以使用js对页面进行设置,禁止右键菜单.禁止选中.禁止复制等功能可以有效的达到这个效果,js代码如下所示: /** * 禁用右键菜单 */ documen ...
- 对oracle中SQL优化的理解
Oracle数据库里SQL优化的终极目标就是要缩短目标SQL语句的执行时间.要达到上述目的,我们通常只有如下三种方法可以选择:1.降低目标SQL语句的资源消耗.2.并行执行目标SQL语句.3.平衡系统 ...
- ReentrantLock可重入锁的原理及使用场景
摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来说容易理解一些. 场景1:如果已加锁,则不再重复加锁 a.忽略重复加锁.b.用在界面交互时点击执行较长时间请求操作时,防止多次 ...
- C语言课堂题集
1.输入一个整数,然后将其数字位置颠倒. int c,d=0,e; scanf("%d",&c); while (c!=0) { e=c%10; d=d*10+e; c/= ...
- Always On 集群监听创建失败问题
创建SQL Server Always On监听失败如下 需要增加:1.登陆服务器的域账号创建计算机对象的权限,2.集群名称账号在域环境中创建计算机对象的权限 如果是重建监听:需要3.先删除原先的监听 ...
- url映射
#include<iostream> #include<algorithm> #include<ctype.h> #include<string> #i ...
- NYOJ 棋盘覆盖
数字很大,要用大数乘法. #include<iostream> #include<stdio.h> #include<string.h> #include<q ...
- [django]模板中自定义变量&django模板中的变量
django自定义模板变量 context_processors.py def mysetings(request): return { 'NAME': 'maotai' } settings.py ...