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 ...
随机推荐
- C#连接Oracle方式
1.在web.config或者app.config文件中,不会像连接SqlServer的格式那样,直接把账号密码写入就可以了 C#是微软的,oracle是Oracle公司做出来的,两家是对立的,都懂得 ...
- AutoMapper在项目中的应用
一.先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说白了就是一个对象,只不过里边全是数据而已. 为什么要用DTO? 1.DTO更注重数据,对领 ...
- MVC Helper Methods
在.net MVC中经常会见到特别的写法 比如 @Url.Action( ) @Url.Conyent( ) @Html.Displayfor( )等等 这些 写法就是我们这里要讲的 Helper ...
- javascript中常见错误类型
js中控制台报错主分两大类: 第一类:语法错误,这一类错误在javascript预解析的过程中如果遇到,则会导致整个js文件都无法执行. 另一类:统称为异常,这一类的错误会导致在错误出现的那一行之后的 ...
- JS函数机制小结
1.javascript中函数是第一型对象,即与其它对象一样,具有: 1.可以通过字面量创建 2.可以赋值给变量或者属性 3.可以作为参数进行传递 4.可以作为函数结果返回 5.拥有属性和方法 2.函 ...
- TNS-12549问题分析及解决办法
该服务器启动监听时候报错因为最后一句是Linux Error:No space left on device 因为是LINUX Error,所以可以到/var/log/messages里查看具体报错信 ...
- Windows Win7建立wifi热点,手机共享WIFI上网
Win7建立wifi热点,手机共享wifi上网 by:授客 QQ:1033553122 1.以管理员身份运行命令提示符:快捷键win+R→输入cmd→回车 2.启用并设定虚拟WiFi网卡:运行命令:n ...
- Wireshark wireshake数据包分割及捕包过滤器介绍
wireshake数据包分割及捕包过滤器介绍 by:授客 QQ:1033553122 wireshake自带工具editcap分割数据包 操作: 进入到目录,然后 editcap.exe -c < ...
- AIDL安卓接口定义语言
Android Interface Definition Language简称AIDL翻译为 :安卓 接口 定义 语言 AIDL:进程间通信.Android ...
- beego快速入门
beego的官方网址:https://beego.me 参考文档:https://beego.me/quickstart 1:安装 您需要安装 Go 1.1+ 以确保所有功能的正常使用. 需要已经设置 ...