【LeetCode】018 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]
]
题解:
这个题与3Sum类似,求4Sum就在原基础上再加上一层循环就可以了。这里只给出此种解法思路的其中一个解法。
Solution 1 (36ms)
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size(); for(int i=; i<n-; i++) {
for(int j=i+; j<n-; j++) {
int k = j+, l = n-;
int a = nums[i], b = nums[j];
while(k<l) {
int c = nums[k], d = nums[l];
if(a+b+c+d == target) {
sv.insert({a,b,c,d});
k++;
l--;
}
else if(a+b+c+d < target) k++;
else l--;
}
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
};
还有一种解法,也是利用了3Sum,不过不是再加一层循环,而是直接调用3Sum函数:取nums[i],然后对后续剩余数组元素求3Sum,tar为target - nums[i];
Solution 2 (32ms)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &nums, int target) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size(); for(int i=; i<n-; i++) {
int a = nums[i];
int j = i+, k = n-;
while(j<k) {
int b = nums[j], c = nums[k];
if(a+b+c == target) {
sv.insert({a,b,c});
j++;
k--;
}
else if(a+b+c > target) k--;
else j++;
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> vv;
sort(nums.begin(), nums.end());
int n = nums.size(); for(int i=; i<n-; i++) {
if(i> && nums[i] == nums[i-]) continue;
//截取剩余数组
vector<int> v(nums.begin()+i+,nums.end());
vector<vector<int>> tmp = threeSum(v, target - nums[i]);
for(int j=; j<tmp.size(); j++) {
tmp[j].insert(tmp[j].begin(), nums[i]);
vv.push_back(tmp[j]);
}
}
return vv;
}
};
还有一种更为优化的解法,思路是一致的,只是加了一个小技巧:在两个外循环中先判断四个值的和与target的大小,即
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
if(nums[i]+nums[n-3]+nums[n-2]+nums[n-1]<target) continue;
通过这两条语句减少了搜索时间,不必进入内循环判断;对于j同理。
Solution 3 (12ms)
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size();
if(n<) return vector<vector<int>> (sv.begin(),sv.end());
for(int i=; i<n-; i++) {
if(nums[i]+nums[i+]+nums[i+]+nums[i+]>target) break;
if(nums[i]+nums[n-]+nums[n-]+nums[n-]<target) continue;
if(i>&&nums[i]==nums[i-]) continue;
for(int j=i+; j<n-; j++) {
if(j>i+&&nums[j]==nums[j-]) continue;
if(nums[i]+nums[j]+nums[j+]+nums[j+]>target) break;
if(nums[i]+nums[j]+nums[n-]+nums[n-]<target) continue;
int k = j+, l = n-;
int a = nums[i], b = nums[j];
while(k<l) {
int c = nums[k], d = nums[l];
if(a+b+c+d == target) {
sv.insert({a,b,c,d});
k++;
l--;
}
else if(a+b+c+d < target) k++;
else l--;
}
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
};
Solution 4
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for(int i = ; i < n - ; ++i){
for(int j = i + ; j < n - ; ++j){
int begin = j + , end = n - ;
while(begin < end){
int sum = nums[i] + nums[j] + nums[begin] + nums[end];
if(sum == target)
res.push_back({nums[i], nums[j], nums[begin++], nums[end--]});
else if (sum < target)
++begin;
else
--end;
}
}
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
};
去重的另一种方法, 使用了algorithm
Solution 5
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
unordered_multimap<int, pair<int, int>> map;
for(int i = ; i < n - ; ++i){
for(int j = i + ; j < n; ++j){
map.insert(make_pair(nums[i] + nums[j], make_pair(i, j)));
}
}
for(auto i = map.begin(); i != map.end(); ++i){
int val = target - i->first;
auto range = map.equal_range(val);
for(auto j = range.first; j != range.second; ++j){
auto a = i->second.first, b = i->second.second;
auto c = j->second.first, d = j->second.second;
if(a != c && a != d && b != c && b != d){
vector<int> tmp = {nums[a], nums[b], nums[c], nums[d]};
sort(tmp.begin(), tmp.end());
res.push_back(tmp);
}
}
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
};
先缓存两个数的和,注意要使用multimap(from 九章算法)
【LeetCode】018 4Sum的更多相关文章
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- 【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 ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】16. 4Sum
题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 【LeetCode】18. 4Sum
题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...
- 【LeetCode】454 4Sum II
题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are su ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
随机推荐
- 让intellij挂在异常处,特别是出现null pointer的地方
1 在Intellij中设置java exception breakpoint 在调试模式下,run->view breakpoints 在java exception breakpoints- ...
- 微信小程序报“app.json”错误解决办法
1.亲测 “app.json未找到入口 app.json 文件,或者文件读取失败,请检查后重新编译.” 是由于新创建的界面xxx.json所在的文件夹为0KB造成的,你可以试着在xxx.json文件内 ...
- Javascript对数组的操作--转载
在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...
- Effective Java 读书笔记(一):使用静态工厂方法代替构造器
这是Effective Java第2章提出的第一条建议: 考虑用静态工厂方法代替构造器 此处的静态工厂方法并不是设计模式,主要指static修饰的静态方法,关于static的说明可以参考之前的博文&l ...
- YY大厅接受不到documentcompleted事件处理
多玩大厅在接受到了页面的documentcompleted事件,才会把遮在页面前面的YY游戏中去掉,我们的游戏页面,YY大厅接收不到事件,所以就排查了下 发现原因在于js脚本里有个用iframe做上报 ...
- python基础8 -----迭代器和生成器
迭代器和生成器 一.迭代器 1.迭代器协议指的是对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2. ...
- spring 注解事务
前提:在applicationContext.xml中配置<tx:annotation-driven transaction-manager="transactionManager&q ...
- iOS9 - 采用3D Touch
iPhone 6s/6s Plus提供了触摸屏的另一个维度的操作手势-3D Touch,通常有下面两种应用场景: 在主屏幕上重按APP图标可以提供进入APP特定功能的快捷菜单 在APP内部,可以通过重 ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- PyVim
PyVim主要用于连接到 Service Instance import atexit from pyVim import connect // Connect to Server If args.d ...