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 ...
随机推荐
- VGA IP核的制作
今天看了本<系统晶片设计-使用NIOS>这本书,看到VGA IP核的设计不错,特移植到Cyclone III上来,试验一下效果. 顶层代码:binary_VGA.v module bina ...
- 用Objective-C的Category特性添加类的属性
http://www.cnblogs.com/wupher/archive/2013/01/05/2845338.html Category是Objective-C中常用的语法特性,通过它可以很方便的 ...
- GCC的编译和安装 很好的资料
http://blog.csdn.net/yrj/article/details/492404 1.GCC的编译和安装2.预处理 #define 可以支持不定数量的参数. 例子如下: ...
- Oracle中trunc函数、round 函数、ceil函数和floor函数的使用
Oracle中trunc函数.round 函数.ceil函数和floor函数的使用 1.1trunc函数处理数字 trunc函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定 ...
- jQuery选择器总结 转
jQuery选择器总结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- .net对js和css、img剥离项目进行压缩优化、cdn加速
由于网站首页以及经常用的页面初始化慢,想后面想了对image.js和css进行迁移优化. 1.把他放到独立的域名上面,这个就要对image,js和css从原项目上面脱离,以及把原来很多页面引用的地址修 ...
- SQL 分组 加列 加自编号 自编号限定 分类: SQL Server 2014-11-25 15:41 283人阅读 评论(0) 收藏
说明: (1)日期以年月形式显示:convert(varchar(7),字段名,120) , (2)加一列 (3)自编号: row_number() over(order by 字段名 desc) a ...
- 第一贱-UILabel
UILabel *label = [[UILabel alloc]init]; label.frame = CGRectMake(100, 100, 100, 100); label.text = @ ...
- spring aop 如何切面到mvc 的controller--转载
原文:http://yjian84.iteye.com/blog/1920787 网上搜罗半天,不知道什么原因,看了源码,好像他们说的controller 是不受代理的,也对哈,不知道怎么办,于是在h ...
- iOS-设置状态栏白色以及覆盖状态栏
iOS-设置状态栏白色以及覆盖状态栏 将状态栏设置为白色 首先, 在info.plist中添加一个标记. View controller–based status bar appearance键值设置 ...