三数之和

给定一个包含 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. PAT——1046. 划拳

    划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...

  2. 提交json串格式的POST请求

    提交json串格式的POST请求 Action() { web_reg_save_param("retCode", "LB=retCode\":\"& ...

  3. HDU1005 Number Sequence(找规律,周期是变化的)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  4. 关于iPad上模态显示视图中的UITextField,UITextView在输入完成后无法回收键盘的问题解决。

    在iPad开发过程中遇到一个问题,UITextField 存在由UIModalPresentationFormSheet 弹出的带导航条的视图控制器中时,调用 resignFirstResponder ...

  5. 学习VCL之路(1)

    在TObject类中,有一个Dispatch()方法和一个DefaultHandler()方法,它们都是与消息分发机制相关的. Dispatch()负责将特定的消息分发给合适的消息处理函数.首先它会在 ...

  6. java程序陷阱

    1.找奇数

  7. first-child伪类选择器

    原文链接地址:https://www.cnblogs.com/wangmeijian/p/4562304.html :first-child 选择器用于选取属于其父元素的首个子元素的指定选择器.——w ...

  8. 从对集合数据去重到Distinct源码分析

    今天在写代码的时候要对数据进行去重,正打算使用Distinct方法的时候,发现这个用了这么久的东西,竟然不知道它是怎么实现的,于是就有了这篇文章. 使用的.net core2.0 1.需求 假如我们有 ...

  9. input的默认样式去除

    outline:none;-----可去除input=text,的输入框输入时的亮边.

  10. 弄清Spark、Storm、MapReduce的这几点区别才能学好大数据

    很多初学者在刚刚接触大数据的时候会有很多疑惑,比如对MapReduce.Storm.Spark三个计算框架的理解经常会产生混乱. 哪一个适合对大量数据进行处理?哪一个又适合对实时的流数据进行处理?又该 ...