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 ...
随机推荐
- Charles设置抓取https请求
1.在手机上设置HTTP代理,将手机与电脑连接. 打开手机设置-WLAN,设置代理 2.(iPhone)使用iPhone自带的浏览器打开Safari,并输入地址:http://charlesproxy ...
- (转载)android开发常见编程错误总结
首页 › 安卓开发 › android开发 android开发常见编程错误总结 泡在网上的日子 / 文 发表于2013-09-07 13:07 第771次阅读 android,异常 0 编辑推荐:稀 ...
- android系统源码下载
ubuntu 安装git curl python 确保主目录下有一个 bin/ 目录,并且该目录包含在路径中: mkdir ~/bin PATH=~/bin:$PATH 下载 Repo 工具,并确 ...
- jq+mui 阻止事件冒泡
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- DataGridView属性设置汇总
1.标题列居中 外观 ColumnHeadersDefaultCellStyle - Alignment - MiddleCenter 2.表格内容居中 外观 DefaultCellStyle - ...
- 提示“CD/DVD找不到媒体所需的驱动”
最近在帮我姐安装win7系统时提 示“CD/DVD找不到媒体所需的驱动”,我用的是U盘安装方式,觉得奇怪,那个镜像文件我已经安装过几十次都没有出错,显然是不会有错的.但是新买的电 脑又不会太大的问题, ...
- docker批量删除容器、镜像
1.删除所有容器 docker rm `docker ps -a -q` docker rm $(docker ps -aq) 2.删除所有镜像 docker rmi `docker images - ...
- sklearn学习汇总
该博主总结的很好,https://www.cnblogs.com/hellcat/p/7531789.html 1.kaggle给出的导图 2.转化成树图: 3.sklearn工具导图
- Webpack 学习记录之概念
1 什么是webpack webpack是一个模块打包器,可以递归的构建一个依赖关系图,其中包含每个程序需要的每个模块,然后将所有模块打包成一个或多个bundle.他和其他的工具最大的不同在于他支持c ...
- Python for Xpath
# Xpath- 在XML文件中查找信息的一套规则/语言,根据XML的元素或者属性进行遍历 ## Xpath开发工具- 开源的Xpath表达式编辑工具:XMLQuire- Chrome插件:Xpath ...