18. 4Sum[M]四数之和
题目
Given an array nums of n integers and an integer target, are there elements a, b, c 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]
]
思路
本题思路很简单,有了前面3Sum的基础,这里只要将 target-d,然后就是3Sum的解题方法。
C++
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > result;
if(nums.size() < 4)
return result;
sort(nums.begin(),nums.end());
int pMid = 0;
int pEnd = 0;
for(int i = 0;i<nums.size() - 3; i++){
//转化成3Sum问题
int subTarget=target - nums[i];
if(i > 0 && nums[i] == nums[i-1])
continue;
for(int j = i + 1;j<nums.size()-2;j++){
int subTarget2 = subTarget - nums[j];
pMid = j + 1;
pEnd = nums.size() - 1;
if(j > i + 1 && nums[j] == nums[j-1])
continue;
while(pMid < pEnd){
int sum1 = nums[pMid] + nums[pEnd];
if(sum1 < subTarget2){
pMid ++;
}
else if(sum1 > subTarget2){
pEnd --;
}
else{
vector<int> tempVec(4,0);
tempVec[0] = nums[i];
tempVec[1] = nums[j];
tempVec[2] = nums[pMid];
tempVec[3] = nums[pEnd];
result.push_back(tempVec);
while(pMid < pEnd && nums[pMid] == nums[pMid+1]) //去除重复元素
pMid ++;
while(pMid < pEnd && nums[pEnd] == nums[pEnd -1])
pEnd --;
pMid ++;
pEnd --;
}
}
}
}
return result;
}
Python
18. 4Sum[M]四数之和的更多相关文章
- 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 ...
- [LeetCode] 454. 4Sum II 四数之和II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [LeetCode] 454. 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- LeetCode(18):四数之和
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...
- 力扣 ——4Sum (四数之和)python 实现
题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
随机推荐
- 有关Gradle Network is unreachable: connect的报错
项目Gradle Errer:Network is unreachable: connect 同时还有as的 报错 Internal HTTP server disabled: Cannot st ...
- 单元测试工具 unitils
Unitils模块组件 Unitils通过模块化的方式来组织各个功能模块,采用类似于Spring的模块划分方式,如unitils-core.unitils-database.unitils-mock等 ...
- SpringMVC(一) HelloWorld
学习新东西的的第一个程序--HelloWorld,以下是SpringMVC的HelloWorld 第一步: 用MAVEN 创建webapp,并添加依赖.(强烈建议使用MAVEN,MAVEN学习书籍和视 ...
- C# 遍历对象下的 属性
foreach (System.Reflection.PropertyInfo p in users.GetType().GetProperties()) { var xx = p.Name; var ...
- 怎么让composer加速(转)
composer 在install的时候会做这几个事情: 去packagist.org中寻找对应需要的包的版本信息和下载地址 循环下载对应的包 解压安装对应的包 我们平时使用composer慢就可能在 ...
- [NOI2005]瑰丽华尔兹_动态规划_单调队列
Code: #include<cstdio> #include<cstring> #include<deque> #include<algorithm> ...
- js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
- Vue学习之路第十四篇:v-for指令中key的使用注意事项
1.学前准备: JavaScript中有一个方法:unshift() ,其作用是向数组的开头添加一个或更多元素,并返回新的长度.该方法的第一个参数将成为数组的新元素 0,如果还有第二个参数,它将成为新 ...
- nyoj212-k尾相等数
212-K尾相等数 内存限制:64MB时间限制:3000msSpecial Judge: No accepted:0submit:0 题目描述: 输入一个自然数K(K>1),如果存在自然数M和N ...
- [JoyOI] 1035 棋盘覆盖 (二分图匹配)
题目描述 给出一张nn(n<=100)的国际象棋棋盘,其中被删除了一些点,问可以使用多少12的多米诺骨牌进行掩盖. 输入格式 第一行为n,m(表示有m个删除的格子) 第二行到m+1行为x,y,分 ...