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 = 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(中等)的更多相关文章
- [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 四数之和
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 (JAVA)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
随机推荐
- linux下的Shell编程(5)循环
Shell Script中的循环有下面几种格式: while [ cond1 ] && { || } [ cond2 ] -; do - done for var in -; do - ...
- OAuth2.0学习(1-9)新浪开放平台微博认证-web应用授权(授权码方式)
1. 引导需要授权的用户到如下地址: URL 1 https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&respons ...
- mysql(3)—— 内连接、外连接的区别
先来看一下,内连接的语法: SELECT XXX FROM XXX INNER JOIN XXX ON XXX; 这里 INNER 可以省略,在上一篇博客中我们对于笛卡尔积现象的研究中(http:/ ...
- JavaScript利用数组原型,添加方法实现遍历多维数组每一个元素
原型就是提供给我们为了让我们扩展更多功能的. 今天学习了用js模拟底层代码,实现数组多维的遍历.思想是在数组原型上添加一个方法. // js中的数组forEach方法,传入回掉函数 能够帮助我们遍历数 ...
- 解析 Javascript - this
在函数中 this 到底取何值,是在函数真正被调用执行的时候确定下来的,函数定义的时候确定不了. 因为 this 的取值是执行上下文环境的一部分,每次调用函数,都会产生一个新的执行上下文环境.当你在 ...
- 电脑上的安卓系统——PhoenixOS浅度体验
前言 其实这篇关于PhoenixOS的浅度评测在几个月前就准备发了,当时是刚看到新闻说Android 7.0 x86的正式版刚刚发布,于是就下载来安装一番,结果.....体验极差= =,只能用这4个字 ...
- PhantomJS命令行选项
支持命令行选项有: --help或-h列出所有可能的命令行选项.立即停止,不会运行一个脚本作为参数传递. --version或-v打印的版本PhantomJS.立即停止,不会运行一个脚本作为参数传递. ...
- typeof与instanceof的区别
一.instanceof运算符: 此运算符可以判断一个变量是否是某个对象(类)的实例,返回值是布尔类型的.想要理解它的作用,必须对面向对象有所理解: 代码实例如下: var str=new ...
- 字段从varchar2修改为number,字段中的内容做修改,替换
#测试表的内容结构:如下所示: 其中DATEHH字段:代表时间,字段在表中是varchar2格式 现有如下需求:字段类型,从varchar2改变为number, 字段中 '.'去除, 2013103少 ...
- 【推荐】CentOS修复OpenSSH用户枚举漏洞
注:以下所有操作均在CentOS 6.8 x86_64位系统下完成. #漏洞说明# OpenSSH(OpenBSD Secure Shell)是OpenBSD计划组所维护的一套用于安全访问远程计算机的 ...