Given an array nums of n integers and an integer target, are there elements abc, 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(双指针)的更多相关文章

  1. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

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

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

  5. 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 = ...

  6. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

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

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

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

随机推荐

  1. [dpdk] TSC , HPET, Timer, Event Timer,RDTSCP

    关于dpdk timer跨越CPU core调度的准确性问题 首先dpdk的timer接口里边使用 cpu cycle来比较时间.根据之前的内容 [dpdk] dpdk --lcores参数 当一个E ...

  2. 转:CSS设置HTML元素的高度与宽度的各种情况总结

    1.元素不设宽度第一种情况:元素为文档流中元素<!-- 父元素宽度为100px --><div style="width:100px;">     < ...

  3. pandas基础运算

    重新索引 (1)reindex重新索引,在已有的索引基础上新建索引,fill_value可以指定新建索引默认值 (2)#新建索引,如果新建的索引值为空自动填充之前的值 对于DataFrame重新索引同 ...

  4. rar压缩类

    using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...

  5. sqlserver配置允许快照隔离

    ALTER DATABASE TustenaOS SET ALLOW_SNAPSHOT_ISOLATION ON

  6. centos mysql 实战 第一节课 安全加固 mysql安装

    centos mysql  实战  第一节课   安全加固  mysql安装 percona名字的由来=consultation 顾问+performance 性能=per  con  a mysql ...

  7. 20181017 PL/SQL 记录

    1. 配置DB 链接文件,帮助中找到tnsnames.ora文件路径,进行注册数据库信息,登陆即可. 这只是个客户端,具体服务器段数据库情况不清楚. 2.写法区别PL/SQL 和SQL 变量定义 赋值 ...

  8. 20170803 Airflow自带的API进行GET 和POST动作部分内容

    --1 首先你要有安装好的Airflow 环境并且在配置文件中有启用API 属性 --2 就是GET 和POST 方法的调用了 这里说一下,由于Airflow在网络上的资料比较少,可以从GETHUB中 ...

  9. 多线程之共享变量.md

    共享变量 - 共享变量:当多个线程同时访问一个变量的时候,会产生共享变量的问题 - 案例11 - 解决变量:锁.信号灯: - 锁(Lock): - 是一个标志,表示一个线程在占用一些资源 - 使用方法 ...

  10. 怎样打开U盘最安全

    为了避免电脑使用U盘时,通过双击,或者右击盘符时,导致把病毒感染至整个电脑,因此使用下面的方法,可使U盘病毒不被激活传播. 在取消了U盘自动运行的情况下(在组策略中一定要关闭自动运行功能,否则只要一插 ...