LeetCode 018 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 = 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)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
代码如下:
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once.
vector<vector<int>> res;
int numlen = num.size();
if(num.size()<4)
return res;
sort(num.begin(),num.end());
set<vector<int>> tmpres;
for(int i = 0; i < numlen; i++){
for(int j = i + 1; j < numlen; j++){
int begin = j+1;
int end = numlen-1;
while(begin < end){
int sum = num[i]+ num[j] + num[begin] + num[end];
if(sum == target){
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(num[begin]);
tmp.push_back(num[end]);
tmpres.insert(tmp);
begin++;
end--;
}
else if(sum<target) begin++;
else end--;
}
}
}
set<vector<int>>::iterator it = tmpres.begin();
for(; it != tmpres.end(); it++)
res.push_back(*it);
return res;
}
};
LeetCode 018 4Sum的更多相关文章
- 【JAVA、C++】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 = 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】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 = ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- [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 ...
- 018 4Sum 四个数的和
给定一个含有 n 个整数的数组 S,数列 S 中是否存在元素 a,b,c 和 d 使 a + b + c + d = target ?请在数组中找出所有满足各元素相加等于特定值的不重复组合.注意:解决 ...
- [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. 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 ...
随机推荐
- ERP的财务凭证的操作与设计--开源软件诞生23
赤龙ERP财务凭证讲解--第23篇 用日志记录"开源软件"的诞生 [进入地址 点亮星星]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redra ...
- .NetCore操作MongDB简要代码实现
.NetCore操作MongoDB简要代码实现 在接触过的大多数使用mongodb的情景中,基本上都是用mongodb来存储日志的. mongodb是作为一种文档型的数据库,在管理日志文档上确实比较适 ...
- CF1295E Permutation Separation
线段树 难得把E想出来,写出来,但却没有调出来(再给我5分钟),我的紫名啊,我一场上紫的大好机会啊 首先考虑是否能将$k$在$1$--$n-1$的每一个的最小代价都求出来 因为$k$从$i$到$i-1 ...
- 13、form组件
Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...
- 安装node.js和vue
1.在官网上下载Node.js安装包 https://nodejs.org/zh-cn/ 2.点击安装,一直下一步下一步就行,这里就不在赘述了. 3.安装完之后,如果没有选安装路径的话,一般都是在[ ...
- Pandas_工资集处理
import numpy as np import pandas as pd from pandas import Series,DataFrame # 1--读取数据文件 file_obj=open ...
- gcc入门(上)
gcc:GNU Compiler Collection的缩写.最初是作为C语言的编译器,现在已支持多种语言.Gcc支持多种硬件平台.gcc是一个可移植跨平台编译器gcc还能跨平台交叉编译器.gcc有多 ...
- prometheus函数介绍
一 函数介绍 gauge类型的数据 属于随机变化数值,并不像counter那样 是 持续增长 1 increase() increase 函数 在promethes中,是⽤来 针对Counter 这 ...
- DB2 建表,添加字段,删除字段,修改字段等常用操作
转载:http://blog.sina.com.cn/s/blog_67aaf4440100v01p.html,稍作修改. --创建数据库 create database Etp; --连接数据库 c ...
- JVM全方位解读(附面试题)
java中就虚拟机是其他语言编写的(C语言+汇编语言,因此,JVM最常出现的攻击就是buffer overflow),如javac命令等,而java api是java写的,大多开源在openjdk,j ...