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. [administrative] windows 下制作USB启动盘的工具

    arch魔教的文档:  https://wiki.archlinux.org/index.php/USB_flash_installation_media windows 下的 dd: https:/ ...

  2. 半屏控制器,view: UIViewController+KNSemiModal

    半屏控制器,view:  UIViewController+KNSemiModal https://github.com/kentnguyen/KNSemiModalViewController

  3. LeetCode 496 Next Greater Element I 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

  4. LeetCode 832 Flipping an Image 解题报告

    题目要求 Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the ...

  5. python 随机模块常用命令

    import randomprint(random.random()) #用于生成一个0到1之间的随机浮点数print(random.uniform(1,3))# 用于生成一个指定范围内的随机浮点数p ...

  6. Bootstrap3隐藏滑动侧边栏菜单代码特效

    链接:https://pan.baidu.com/s/1syV3ZFg-RqfCv0HS5K0vug 提取码:yjex

  7. redis安装详解

    一.redis安装步骤: 1.首先上官网下载Redis 压缩包,地址:http://redis.io/download 下载稳定版3.0.7即可.2.通过远程管理工具,将压缩包拷贝到Linux服务器中 ...

  8. pyqt5-对文本样式进行操作

    self.label_2 = QtWidgets.QLabel(self.centralWidget) self.label_2.setGeometry(QtCore.QRect(330, 220, ...

  9. C++ 方阵原地旋转90度

    不额外申请内存(另外的一个二维数组空间),将一个方阵(二维数组)原地旋转90度,主要的思路是,由外向内,一圈圈的进行旋转(就是依次进行交换),如下图所示,当这些圈圈都交换完了之后,就完成了原地旋转了. ...

  10. 各大知名区块链交易所链接及API文档链接

    区块链交易所链接 火币网(Huobi):https://www.huobi.br.com/zh-cn/ API文档:https://github.com/huobiapi/API_Docs/wiki ...