[leetcode]三数之和
三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为
[[-1, 0, 1],[-1, -1, 2]]
暴力解超时
思路:选定一个值,两侧逼近求两数和
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>> res;
if(nums.size()>2)
{
for(int i=0;i<nums.size()-1;i++)
{
if(nums[i]>0)
break;
int j=i+1;
int k=nums.size()-1;
int target=-nums[i];
while(j<k)
{
if(nums[j]+nums[k]==target)
{
vector<int> temp = { nums[i],nums[j],nums[k]};
res.insert(temp);
j++,k--;
}
if(nums[j]+nums[k]>target)
k--;
if(nums[j]+nums[k]<target)
j++;
}
}
}
return vector<vector<int>>(res.begin(),res.end());
}
[leetcode]三数之和的更多相关文章
- LeetCode 三数之和 — 优化解法
LeetCode 三数之和 - 改进解法 题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复 ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况
Given an integer array A, and an integer target, return the number of tuples i, j, k such that i &l ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
随机推荐
- 【题解】洛谷P1073 [NOIP2009TG] 最优贸易(SPFA+分层图)
次元传送门:洛谷P1073 思路 一开始看题目嗅出了强连通分量的气息 但是嫌长没打 听机房做过的dalao说可以用分层图 从来没用过 就参考题解了解一下 因为每个城市可以走好几次 所以说我们可以在图上 ...
- HDU 3938 Portal (离线并查集,此题思路很强!!!,得到所谓的距离很巧妙)
Portal Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- ArcMap中用python的split方法提取字段的值
提取PROPERTY_L字段空格分隔符前面的地址编号 提取前:5105 ABERDEEN LANE 提取后:5105 提取的表达式:!PROPERTY_L!.split(" ")[ ...
- LeetCode40.组合总和|| JavaScript
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- ORA-10485: Real-Time Query cannot be enabled while applying migration redo
情景:利用Dataguard滚动方式升级数据库后,备库应用redo报错:ORA-10485 MRP0: Background Media Recovery terminated with error ...
- VS2015菜单栏重复删除
举个例子,这个是工具栏的,出现了重复 只要选择工具栏自定义那个选项,在多余命令的下方,先删除几个外部命令,然后把空行删除,最后全部重置即可 结果如下图
- 15JavaScript switch语句
1.JavaScript switch 语句 使用 switch 语句来选择要执行的多个代码块之一. 语法: switch(n) { case 1: 执行代码块 1 break; case 2: 执行 ...
- react-navigation的超级大坑
本文针对react-navigation^3.0.0版本,版本不对的话,请不要看本文,直接看官方英文文档 最近一直在学习RN,没找到什么好的视频,所以一直看文档,一路上来虽然遇到一些乱七八糟的bu ...
- CentOS6安装各种大数据软件 第三章:Linux基础软件的安装
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- Hadoop源码学习笔记之NameNode启动场景流程四:rpc server初始化及启动
老规矩,还是分三步走,分别为源码调用分析.伪代码核心梳理.调用关系图解. 一.源码调用分析 根据上篇的梳理,直接从initialize()方法着手.源码如下,部分代码的功能以及说明,已经在注释阐述了. ...