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 ...
随机推荐
- 搭建go-stress-testing压力测试
参考地址:https://github.com/link1st/go-stress-testing安装golang环境 yum install -y golang 下载软件包 wget -q http ...
- Java NIO:通道
最近打算把Java网络编程相关的知识深入一下(IO.NIO.Socket编程.Netty) Java NIO主要需要理解缓冲区.通道.选择器三个核心概念,作为对Java I/O的补充, 以提升大批量数 ...
- shell-整数测试多范例多生产案例举例
1. 整数测试举例范例1:整数条件测试举例 root@test-1 ~]# a1=10;a2=13 [root@test-1 ~]# echo $a1 $a2 10 13 [root@test-1 ~ ...
- Acticiti流程引擎在已知当前流程定义id的情况下获取当前流程的所有信息(包括:节点和连线)
这里我们已知流程已经部署,我的需求是获取当前流程的所有任务节点,我使用instanceof关键字来进行匹配 private List<UserTask> getProcessUserTas ...
- Springboot集成JUnit5优雅进行单元测试
为什么使用JUnit5 JUnit4被广泛使用,但是许多场景下使用起来语法较为繁琐,JUnit5中支持lambda表达式,语法简单且代码不冗余. JUnit5易扩展,包容性强,可以接入其他的测试引擎. ...
- 自动创建新序列号的Cookies脚本
已知一个网站在被访问的时候会读取电脑上存储的cookies 如果已经有cookie变量存在 则在存在的变量后按顺序增加新的序列 如电脑上有vst1变量的cookie了 那么新用户则自动创建为 vst2 ...
- 多测师讲解jmeter _安装和配置环境(00)_高级讲师肖sir
1.下载jmeter包,我们已经下载了有现成的: 2.安装jjdk默认安装或自定义安装 默认安装的路径: 如下图 3.第三步:安装完成后配置JDK的环境变量 位置:计算机→属性→高级系统设置→高级→ ...
- 【8】进大厂必须掌握的面试题-Java面试-异常和线程
Q1.错误和异常有什么区别? 错误是在运行时发生的不可恢复的情况.如OutOfMemory错误.这些JVM错误无法在运行时修复.尽管可以在catch块中捕获错误,但是应用程序的执行将停止并且无法恢复. ...
- postgresql 导出数据库与数据表
单表导出 pg_dump --host 127.0.0.1 --port 5432 --username "postgres" --role "postgres" ...
- docker 启动mysql 挂载宿主机目录
在使用docker run 运行镜像获取容器时,有些容器会自动产生一些数据,为了这些数据会因为container (容器)的消失而消失,保证数据的安全,比如mysql 容器在运行中产生的一些表的数据, ...