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 ...
随机推荐
- 小程序通过用户授权获取手机号之getPhoneNumber
小程序有一个获取用户很便捷的api,就是通过getPhoneNumber获取用户的已经绑定微信的手机号码.有一点要大家注意,现在微信和注重用户体验,有些方法都是需要用户主动去触发才能调用的,比如get ...
- js-权威指南学习笔记19
第十九章 jQuery类库 1.传递HTML文本字符串给$()方法,jQuery会根据传入的文本创建好HTML元素并封装为jQuery对象返回. 2.想要遍历jQuery对象中的所有元素时,可以调用e ...
- CSS中文乱码解决方法
原文链接:http://caibaojian.com/css-unicode.html 我的CSS里面有一个content用到了中文,作用主要是在前端日报文章中显示出“网页链接”这四个字,然而打开百度 ...
- 原生css 中变量的使用
前两天看到阮大神的一篇在css中使用变量的文章,整理了一下. 这个重要的 CSS 新功能,所有主要浏览器已经都支持了.本文全面介绍如何使用它,你会发现原生 CSS 从此变得异常强大. 一.变量的声明 ...
- Vue.js之路由系统
Vue.js生态之vue-router vue-router是什么? vue-router是Vue的路由系统,定位资源的,我们可以不进行整页刷新去切换页面内容. vue-router的安装与基本配置 ...
- 从零开始学习html(十四)单位和值
一.颜色值 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <tit ...
- SharePoint Server 2013安装
坑死人不偿命的呀 在Windows Server 2012 R2上安装SharePoint Server 2013,安装了半天,结果卡在“Windows Server AppFabric”安装错误上, ...
- 如何使用活字格快速搭建Bug管理系统?
Bug管理系统是指一种用于添加Bug.修复Bug.测试Bug.删除Bug的一套完整的Bug管理系统. 完整的Bug管理过程包含: 1.测试人员利用Bug管理系统提交发现的bug. 2.测试人员把bug ...
- MySQL——索引优化实战
上篇文章中介绍了索引的基本内容,这篇文章我们继续介绍索引优化实战.在介绍索引优化实战之前,首先要介绍两个与索引相关的重要概念,这两个概念对于索引优化至关重要. 本篇文章用于测试的user表结构: 索引 ...
- ie8 透明背景不能点击问题
最近开发网站,需求是三个一屏,1和3只能看见一半,2显示在中间,无箭头按钮. 因为之前写过一个有前后按钮的插件,想着怎么就在这上面改造,故把前后按钮去掉背景,定位在了1和3的位置上来实现点击前后, 发 ...