三数之和

给定一个包含 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]三数之和的更多相关文章

  1. LeetCode 三数之和 — 优化解法

    LeetCode 三数之和 - 改进解法 题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复 ...

  2. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  7. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  8. [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 ...

  9. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

随机推荐

  1. 【luogu P2245 星际导航】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2245 = 货车运输 被逼着写过mst+lca 后来成了mst+树剖 #include <cstdio& ...

  2. "strace -p"非常有用,它减少了很多猜测工作,也不需要重新启动应用。lsof -p process_id +iostat + sar -n DEV 1

    linux神器strace - youxin - 博客园https://www.cnblogs.com/youxin/p/8837771.html 某个进程突然占用了很多CPU? 或者某个进程看起来像 ...

  3. 书单list

    2018年对我来说是个踩到狗屎的一年,不能说运气差,只能说过去所有的不良决策后果都集中到2018年爆发了,希望新的一年,都好起来.书单很短,买的确实很多,真的惭愧. 昨日世界 南渡北归 上学记 回忆录 ...

  4. redis单节点集群

    一.概念 redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串.哈希.列表.队列.集合结构 ...

  5. ODI使用流程

    ---恢复内容开始--- ODI流程 Topology 1.建立 源 物理结构体系 2.建立 目的 物理结构体系 步骤同上 3.建立 源 逻辑架构 4.建立 目的 逻辑架构 步骤同上 Designer ...

  6. makefile中的一些参数说明

    #obj = main.o sub.o add.o div.o mul.osrc = $(wildcard *.c) #搜索.c文件 可以加路径 obj = $(patsubst %.c, %.o, ...

  7. sql 事务处理 与 try catch

    begin transaction tr declare @error int; ; select * from Car_Brand set @error=@error+@@ERROR set @er ...

  8. 我们比较常见的PHP实现openSug.js参数调试

    这是一款利PHP对百度搜索下拉框提示免费代码实现参数配置调试的程序源代码. 由想要对网站进行搜索下拉调试的站长朋友们进行方便.快速的效果演示,具体参考下面的PHP代码. 如何使用? 请新建一份PHP文 ...

  9. Delphi RAD Server 应用服务基础平台

    RAD Server是一个应用服务框架平台,可快速构建和部署应用服务.RAD Server提供自动化的Delphi和C++ REST/ JSON API的 发布与管理.企业数据库集成中间件.智能物联网 ...

  10. 需求:加一个下拉框选择条件改变饼图内外环 饼图:百度echarts提供

    1.1:下拉框条件:后台取得ViewBag传给前台 MonitorController: public ActionResult BigData(): //下拉框筛选条件 var result = M ...