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 ...
随机推荐
- hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(4)SPARK 安装
hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(4)SPARK 安装 一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh ...
- 【第二十一篇】手C# MVC 微信授权登录 OAuth2.0授权登录
首先一定要熟读,最起码过一遍微信开发者文档 微信开发者文档 文档写的很清楚 授权登录四步走 在正文开始前,我得讲清楚一个事情 敲黑板,划重点:微信一共有两个 access_token 一个是7200就 ...
- GET和POST两种基本请求方法的区别
文章来源:http://www.cnblogs.com/logsharing/p/8448446.html GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一 ...
- 在删除一个指针之后,一定将该指针设置成空指针(即在delete *p之后一定要加上: p=NULL)
在删除一个指针之后,一定将该指针设置成空指针(即在delete *p之后一定要加上: p=NULL)
- html标记语言 --文本标记
html标记语言 --文本标记 二.文本标记 1.h1-h6 标题标记,h1最大 2.font 字体设置标记 2.1 size字体大小.<font size="> 取值范围1-7 ...
- [转]python 模块 chardet下载及介绍
来源:http://blog.csdn.net/tianzhu123/article/details/8187470/ 在处理字符串时,常常会遇到不知道字符串是何种编码,如果不知道字符串的编码就不 ...
- url,href,src之间的区别
发现自己居然没把url.href.src关系及使用搞清楚,今天就理一下.主要包括:url.src.href定义以及使用区别. URL(Uniform Resource Locator) 统一资源定位符 ...
- 笔记本电脑连wifi然后通过有线网口做桥接
让你的笔记本电脑作为主机,台式机通过通过一根网线连接到你的笔记本,共享无线网络上网,可以进行如下操作: 1,先找跟网线将两台电脑连接. 2,打开win7自带的windows防火墙,此步在控制面板里可以 ...
- Multipath在OpenStack中的faulty device的成因及解决(part 1)
| 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 简介: Multip ...
- iOS masonry 不规则tagView布局 并自适应高度
在搜索页面经常会有不规则的tag出现,这种tagView要有点击事件,单个tagView可以设置文字颜色,宽度不固定根据内容自适应,高度固定,数量不固定.总高度就不固定.最近对于masonry的使用又 ...