18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列
思路:采用3Sum的做法
ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要判断重复
图书馆的网,已经到了令人发指的程度,我告诫自己千万不要暴躁。
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> ans;
vector<int> vec();
int k,l,temp;
sort(nums.begin(),nums.end());
for(int i=;i<nums.size();++i){
if(i>&&nums[i]==nums[i-])continue;
for(int j=i+;j<nums.size();++j){
if(j>i+&&nums[j]==nums[j-])continue;
k=j+;
l=nums.size()-;
while(k<l){
if(k>j+&&nums[k]==nums[k-]){
++k;
continue;
}
temp=nums[i]+nums[j]+nums[k]+nums[l];
if(temp>target)--l;
else if(temp<target)++k;
else{
vec[]=nums[i];
vec[]=nums[j];
vec[]=nums[k];
vec[]=nums[l];
ans.push_back(vec);
--l;
++k;
}
}
}
}
return ans;
}
};
18 4Sum(寻找四个数之和为指定数的集合Medium)的更多相关文章
- 15 3Sum(寻找三个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列 思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i.j.k ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- lintcode:四个数之和
题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...
- 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 ...
- 18. 4Sum[M]四数之和
题目 Given an array nums of n integers and an integer target, are there elements a, b, c and d in nums ...
- LeetCode:18. 4Sum(Medium)
1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...
- [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 ...
随机推荐
- 4G来临 IT业转型之路当在不远
摘 要:4G商用未启,品牌营销争夺已经展开.目前,除了中国移动推出全新4G品牌“andM”之外,中国电信和中国联通均选择继续沿用3G的品牌. 4G商用未启,品牌营销争夺已经展开.12月10日,中国电信 ...
- 353. Design Snake Game
贪食蛇. GAME OVER有2种情况,1是咬到自己,2是出界. 1)用QUEUE来保留占据的格子,每走一格就添加1个,然后POll()最后一个. 做一个一样的SET来check要走的格子是不是已经在 ...
- DevExpress LookUpEdit 下拉框基本操作
<span style="font-size:14px;"> ArrayList list = new ArrayList(); //遍历皮肤,放到列表中 foreac ...
- SQL 存储过程 通过多个ID更新数据 分类: SQL Server 2014-12-08 16:08 299人阅读 评论(0) 收藏
下面举个例子说明: 我想让一部分品牌的名称(即Brand_Name)后面加上1,Brand_ID是主键,sql语句很容易实现,但是存储过程如何写呢? 错误写法如下: //*************** ...
- windows下安装pip
1.在安装pip前,请确认win系统中已经安装好了python,和easy_install工具,如果系统安装成功,easy_install在目录C:\Python27\Scripts 下面,确认截图如 ...
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
Applications are driven by state. Many things, like the user interface, should always be consistent ...
- storyBoard使用介绍
storyBoard使用介绍 转载地址:http://www.2cto.com/kf/201210/161737.html 一 .简述 Storyboard是你可以用来定义用户界面的一种新的方式,像x ...
- ICMP协议
1. ICMP简介: ICMP全名为(INTERNET CONTROL MESSAGE PROTOCOL)网络控制报文协议,协议号为1,网络层协议. 它是TCP/IP协议族的一个子协议,用于在IP主机 ...
- css考核点整理(九)-有几种文字替换方式,之间的优缺点
有几种文字替换方式,之间的优缺点
- 最近的两个小项目,2:Python webapp的docker镜像
时间过得真快,一眨眼一个多月没更新了,但这一个月我可没偷懒啊,真的是忙.粘上两篇ReadMe勉强凑合一下,保持博客更新是好习惯. 基于Flask框架,uwsgi起服务,supervisor做管理,应该 ...