Given an array S of n integers, are there elements a, b, c, and d in S 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.

For example, given array S = [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]
]

题目和15题的要求几乎一样,只不过是4Sum.

思路和15题一模一样,但仍要注意去重.

自己思路:

有了3sum的经验这个就好做了,思想和3sum没有区别

先排序;

固定i, 固定j, move lo and hi;

We'll come across 3 cases(sum == ta, sum > ta, sum < ta) and deal with them.

自己代码:

\(O(n^3)\) time, \(O(1)\) extra space.

vector<vector<int>> fourSum(vector<int>& A, int ta) {
const int n = A.size();
vector<vector<int>> res;
sort(A.begin(), A.end()); // 有了3sum的经验这个就好做了,思想和3sum没有区别
// 固定i, 固定j, move lo and hi
// we'll come across 3 cases(sum==ta, sum>ta, sum<ta) and deal with them
for (int i = 0; i < n - 3; i++) {
if (i > 0 && A[i] == A[i - 1]) continue; //去重
for (int j = i + 1; j < n - 2; j++) {
if ((j > i + 1) && (A[j] == A[j - 1])) continue; //去重
int lo = j + 1, hi = n - 1; while (lo < hi) {// lo <= hi 不可以
int sum = A[i] + A[j] + A[lo] + A[hi];
if (sum == ta) { res.push_back({A[i], A[j], A[lo], A[hi]}); while (lo < hi && A[hi] == A[hi - 1]) hi--; //去重
while (lo < hi && A[lo] == A[lo + 1]) lo++; //去重
lo++; // 只有lo++或 只有hi--也可 他俩都没有就无限循环了
hi--; } else if (sum > ta) {
while (lo < hi && A[hi] == A[hi - 1]) hi--; //去重
hi--;
} else { // sum < ta
while (lo < hi && A[lo] == A[lo + 1]) lo++; //去重
lo++;
}
}
}
}
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 四数之和

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

  7. 18. 4Sum (JAVA)

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  8. LeetCode——18. 4Sum

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

  9. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

随机推荐

  1. 从感知机到 SVM,再到深度学习(一)

        在上篇博客中提到,如果想要拟合一些空间中的点,可以用最小二乘法,最小二乘法其实是以样例点和理论值之间的误差最小作为目标.那么换个场景,如果有两类不同的点,而我们不想要拟合这些点,而是想找到一条 ...

  2. ubuntu下创建python的虚拟环境

    当我们在同一个机器上进行开发多个项目,每个项目于用到包的不同版本的时候,就很尴尬. 安装python包的命令是: sudo pip install 包名 这样的话,会将包安装到/usr/local/l ...

  3. mysql中的函数与存储过程

    mysql中的函数:1 mysql下创建函数: 1.1 语法: delimiter $$ -- 设置分隔符,默认是; 设置成其他符号,让编译器知道我们函数编写的结束,此处设置成$$ create fu ...

  4. 双击表,powerdesigner pdm 没有 comment列(no comment)

  5. CMDB开发

    浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...

  6. Spring之IOC(一)

    Spring之IOC(一) 对于Spring的基本概念在这里就不在赘述了. Spring的最核心的两部分则是AOP(面向切面编程)和IOC(控制反转).本篇文章主要讲我对IOC的理解. IOC:即In ...

  7. 小技巧-WEB API第一次加载很慢

    原文:http://www.afuhao.com/article_articleId-219.shtml 摘要:ASP.NET页面首次打开很慢,但别的页面如果没有访问过,去访问也会慢.你也许认为它是在 ...

  8. C++ namespace的作用

    namespace:命名空间或者叫名字空间,传统的c++只有一个全局的namespace,但是由于现在的程序规模越来越大,程序的分工越来越细,全局作用域就变得越来越拥挤,每个人都可能使用相同的名字来实 ...

  9. Windows安装SVN服务器和客户端

    我的操作系统版本是windows10 64位.接下来我会先介绍SVN服务器的安装,然后再介绍安装SVN客户端,并进行测试. 下载 首先我们需要到官网上去下载svn服务器程序. [svn官网地址] (h ...

  10. 【DataMagic】如何在万亿级别规模的数据量上使用Spark

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 作者:张国鹏 | 腾讯 运营开发工程师 一.前言 Spark作为大数据计算引擎,凭借其快速.稳定. ...