leetcode18—4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example:
Given array nums = [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] ]
想法:类似于3Sum的解决方式,设立双指针求解
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int len = nums.size();
vector<vector<int>> result;
== len)
return result;
sort(nums.begin(),nums.end());
; i < len- ; i++){
&& nums.at(i) == nums.at(i-))
continue;
&& nums.at(i) + nums.at(i+)+nums.at(i+)+nums.at(i+) > target)
break;
&& nums.at(i) + nums.at(len-)+nums.at(len-)+nums.at(len-) < target)
continue;
; j < len- ; j++){
&& nums[j] == nums[j-])
continue;
&& nums[i] + nums[j] + nums[j+] + nums[j+] > target)
break;
&& nums[i] + nums[j] + nums[len-] + nums[len-] < target)
continue;
;
;
while(left < right){
int temp = nums.at(i) + nums.at(j) + nums.at(left) + nums.at(right);
if(temp == target){
result.push_back({nums.at(i),nums.at(j),nums.at(left),nums.at(right)});
left++;
right--;
])//避免重复
left++;
])
right--;
}else if(temp < target){
left++;
}else{
right--;
}
}
}
}
return result;
}
};
leetcode18—4Sum的更多相关文章
- [array] leetCode-18. 4Sum -Medium
18. 4Sum -Medium descrition Given an array S of n integers, are there elements a, b, c, and d in S s ...
- LeetCode18 4Sum
题意: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- ARTS第八周
1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有观点和思考的技术文章 以下是 ...
- LeetCode18: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 ...
- [Swift]LeetCode18. 四数之和 | 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- [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 ...
随机推荐
- 悟空模式-java-建造者模式
[此是锻炼神冰铁,磨琢成工光皎洁.老君自己动钤锤,荧惑亲身添炭屑.五方五帝用心机,六丁六甲费周折.造成九齿玉垂牙,铸就双环金坠叶.身妆六曜排五星,体按四时依八节.短长上下定乾坤,左右阴阳分日月.六爻神 ...
- Unix环境高级编程:守护进程
参考 Unix环境高级编程,第9,13章 介绍 守护进程就是Linux中使用ps aux那些一般以d结尾的程序,比如rsyslogd,sshd等,为daemon简称.他们是长期在后台执行的随终端关闭而 ...
- Entity FrameWork(实体框架)是以ADO.NET Entity FrameWork ,简称为EF
Entity FrameWork(实体框架)是以ADO.NET Entity FrameWork ,简称为EF Entity FrameWork的特点 1.支持多种数据库(MSSQL.Oracle.M ...
- js-ES6学习笔记-变量的解构赋值
1.ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 2.ES6允许写成:let [a,b,c] = [1,2,3];上面代码表示,可以从数 ...
- css 文本两行显示,超出省略号表示
重点:text-overflow: ellipsis;只对display:inline:起作用 例子: <span class="a">我说说<b class= ...
- 正则匹配身份证有bug你知道么?
在开发中,我们需要验证用户的输入信息,多半采用正则验证,下面就是身份证证号的几种常用的正则表达式: var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x) ...
- readlink 命令
在Linux中readlink命令的作用是:输出符号链接值或权威文件名(通常使用的是-f参数) 格式:readlink [选项]... 文件 参数: -f, --canonicalize 递归跟随 ...
- Windows桌面.exe程序安装、卸载、升级测试用例
一.安装 1) 系统:XP.win 7.win 8.win 10 2)安全类型软件:360杀毒.360安全卫士.金山毒霸.百度杀毒.腾讯电脑管家等. 3)同类型软件兼容 4)用户名称:中文用户.英文用 ...
- InteliiJ IDEA的安装配置与简单使用
小Alan前段时间一直在家里搬砖,已经很久没有接触技术了,从今天开始重拾技术,工欲善其事,必先利其器,以前在做Java开发的时候最常用的IDE就是Eclipse莫属了,不过随着岁月的流逝,在2016年 ...
- Python3 sqlacodegen 根据已有数据库生成 ORM 使用的 model.py
pip install sqlacodegen pip install pymysql sqlacodegen mysql+pymysql://username:password@127.0.0.1: ...