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 ...
随机推荐
- Mybatis插件开发
前面几篇文章介绍了Mybtis中四个重要的对象,其中提到它们都是在Configuration中被创建的,我们一起看一下创建四大对象的方法,代码如下所示: public ParameterHandler ...
- jquery中 苹果手机对on触发的点击事件无效果
在被点击的元素上加上样式 cursor:pointer; 苹果手机就可以触发事件了
- 通过ssh实现远程登陆服务器!
通过ssh实现远程登陆前提是服务器已经开启ssh服务,至于怎么开启,可以参看上一篇“Linux服务器开启ssh服务,实现ssh远程登陆!”! 使用ssh登陆时,输入主机(linux的ip地址),账号, ...
- 【代码笔记】iOS-4个可以单独点击的button
一,效果图. 二,工程图. 三,代码. ViewController.m #import "ViewController.h" @interface ViewController ...
- cmd--命令短集
查看ip地址:ipconfig 查看ip地址:ipconfig/all 进入c盘program files目录下:cd %Program Files%,”x:“, 进入x盘根目录.cd “ ”进入某文 ...
- Excel indirect引用其它xlsx文件内容作为下拉框
效果如下图: 在第一个excel文件中有一个下拉框 这里面的选项,需要从另外一个Excel文件中读取内容,另外一个Excel文件如下: 实现的步骤如下: 1.新建一个Excel文件select.xls ...
- 测试sql server服务是否配置正确
最简单的方法: 新建一个testdb.udl文件,双击可出现测试界面
- VMware 15 安装 MAC OS 10.13 原版(详细图文教程)
VMware 15 安装 MAC OS 10.13 原版(详细图文教程) 生命在于折腾,之前本想装个双系统黑苹果,什么 U 盘启动盘,四叶草引导,都配置好了,最后跪在一个动态卷上,备份格盘现在弄不了, ...
- Python Django框架笔记(三):django工作方式简单说明和创建用户界面
(一) 说明 简单说明下django的工作方式,并举2个例子. (二) Django工作方式 假定我们有下面这些文件 ,这里在前2篇的基础上增加了 templates目录(存放html文件) 和s ...
- BBR,附CentOS 6/7配置过程
最近这段时间BBR都比较火,前面有说如何在CAC的Debian-8-64bit安装BBR正确打开方式,现在说下,CentOS 6/7配置过程. 推荐理由:没配置BBR前,用SS看U2B的速度206K/ ...