三数之和

给定一个包含 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 P1156 垃圾陷阱】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1156 设\(dp[i][j]\)表示前i堆到达高度j时的所活最长时间 那么一旦到当前状态能到达满足的时间和高 ...

  2. iOS的AssetsLibrary框架访问所有相片

    该框架下有几个类,ALAssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation. ALAssetsLibrary ...

  3. HDU 1561 The more, The Better(树形dp之树形01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1561 The more, The Better Time Limit: 6000/2000 MS (J ...

  4. Jfinal框架登陆页面的图形验证码

    本文转自,http://www.bubuko.com/infodetail-720511.html 验证码的工具类, 这个jfinal自带的也有,但是下面这个和Jfinal自带的有一点点小的改动,(我 ...

  5. JavaScript互斥锁案例

    朋友今天问起来关于JS中多个函数共享同一个全局变量时,顺序调用执行的函数,前者修改了全局变量值,后调用的函数访问时却为undefined. 前不久开发项目过程中,队友也遇到了同样的问题,索性就写份博客 ...

  6. 排列组合:01转换法之lua实现

    c++ 版连接 https://blog.csdn.net/canguanxihu/article/details/46363375 因为项目用的是lua脚本,看了C++版后自己写了一个lua版本的, ...

  7. Oracle11g 行列转换函数PIVOT and UNPIVOT

    作为Oracle开发工程师,推荐大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...

  8. activemq的高级特性:消息的可靠性

    高级特性之消息的可靠性 可靠性分为:生产者.消费者.生产者必须让mq收到消息,消费者必须能够接收到消息并且消费成功,这就是消息的可靠性. 1:生产者可靠性 Session session = conn ...

  9. 【转】C#计算两坐标点距离

    //地球半径,单位米 ; /// <summary> /// 计算两点位置的距离,返回两点的距离,单位 米 /// 该公式为GOOGLE提供,误差小于0.2米 /// </summa ...

  10. Immutable.js 以及在 react+redux 项目中的实践

    来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...