leetcode132:4sum
题目描述
- 四元组(a、b、c、d)中的元素必须按非降序排列。(即a≤b≤c≤d)
- 解集中不能包含重复的四元组。
例如:给出的数组 S = {1 0 -1 0 -2 2}, 目标值 = 0.↵↵ 给出的解集应该是:↵ (-1, 0, 0, 1)↵ (-2, -1, 1, 2)↵ (-2, 0, 0, 2)
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:
- Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- 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)↵
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector <vector<int>> res;
vector< int> temp (4,0);
set<vector<int> > st;
sort(num.begin(),num.end());
int n=num.size();
for (int i=0;i<n;i++){
for (int j=i+1;j<n;j++){
int left=j+1,right=n-1;
while (left<right){
int sum=num[i]+num[j]+num[left]+num[right];
if (sum==target){
temp[0]=num[i];
temp[1]=num[j];
temp[2]=num[left];
temp[3]=num[right];
st.insert(temp);
}
if (sum<target)
left++;
else
right--;
}
}
}
set<vector<int>>::iterator it;
for (it=st.begin();it !=st.end();it++)
res.push_back(*it);
return res;
}
};
leetcode132:4sum的更多相关文章
- [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] 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:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum
18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c ...
- No.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 = ...
- 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 3Sum & 4Sum
3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...
- 【leetcode】4Sum
4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum
2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...
随机推荐
- vue+elementUI实现 分页表格的单选或者多选、及禁止部分选择
一.vue+elementUI实现 分页表格前的多选 多选效果图: 代码如下: <el-table ref="multipleTable" :data="listD ...
- 项目使用eslint
今天eslint版本更新了,然后昂,有些奇奇怪怪的错误提示了,然后想,这我得 1.配置一个保存时根据eslint规则自动修复 2.欸,之前编码遇到未使用的变量都会有标记黄线,我很好定位,这会怎么没了 ...
- 实现Excel文件的上传和解析
前言 本文思维导图 一.需求描述 实现一个页面上传excel的功能,并对excel中的内容做解析,最后存储在数据库中. 二.代码实现 需求实现思路: 先对上传的文件做校验和解析,这里我们通过Excel ...
- day24 Pyhton学习 反射
一.isinstance,type,issubclass issubclass() 这个内置函数可以帮我们判断x类是否是y类的子类 issubclass(x,y) class Base: pass c ...
- linux学习(一)--启动文件bootsect.s
这是linux由BIOS加载后执行的第一段的启动程序代码,即文件 boot/bootsect.s 首先附图,简单介绍一下从开机加电到第一段linux代码执行的简要过程 1 .globl begte ...
- nginx 是如何处理过期事件的?
目录 什么是过期事件 nginx 是如何处理过期事件的? 参考资料 什么是过期事件 对于不需要加入到 post 队列 延后处理的事件,nginx 的事件都是通过 ngx_epoll_process_e ...
- 正则匹配img标签 蜘蛛 爬取分析 新闻采集
string ostr = "aaaaaa<img asddsa src=\"\" asddsasd />aaaaaaa<img src=\" ...
- NCEP数据资料获取 地面抬升指数
先放上数据地址:https://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.surface.html 美国国家环境预报中心(NCEP ...
- 数据库备份作业的T-SQL语句
1.关于大容量数据导入导出的一些方法SQL SERVER提供多种工具用于各种数据源的数据导入导出,这些数据源包括本文文件.ODBC数据源.OLE DB数据源.ASCII文本文件和EXCEL电子表格.2 ...
- linux centos8 安装dokcker并启动coreapi
粘的个人笔记,格式有点乱.勿在意 core api程序包 发布直接部署包: 链接:https://pan.baidu.com/s/1zZe9H1Fevf7DdzfF-MJb9w 提取码:t0ai 源码 ...