Leetcode--3Sum
Problem Description:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},A solution set is:(-1, 0, 1)(-1, -1, 2)
分析:题目要求把全部可能的集合都找出来。因此想到的就是首先将数组排序,然后利用两重循环依次选出两个数字a和b。然后在剩下的数字中查找是否存在c,详细实现用到了upper_bound函数找到比当前数大的第一个数,去掉反复循环的情况。然后用find函数查找c是否存在,存在则将三个数记录下来。
详细代码例如以下:
class Solution {
public:vector<vector<int> > threeSum(vector<int> &num) {vector<vector<int> > results;if(num.size()<3) return results;vector<int> subset;sort(num.begin(),num.end());vector<int>::iterator p=num.begin(),q,flag;while(p<(num.end()-2)){q=p+1;while(q<num.end()-1){int tag=0-*p-*q;if(find(q+1,num.end(),tag)!=num.end()){flag=find(q+1,num.end(),tag);subset.push_back(*p);subset.push_back(*q);subset.push_back(*flag);results.push_back(subset);}subset.clear();q=upper_bound(q,num.end()-1,*q);}p=upper_bound(p,num.end()-2,*p);}return results;}
};
转载于:https://www.cnblogs.com/liguangsunls/p/6880124.html
Leetcode--3Sum相关推荐
- 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 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 -- 3Sum
Question: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F ...
- LeetCode - 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- leetcode 3Sum C++
荒废好久没更新了,时间过得很快,转眼就2017年了,经历了苦闷的科研阶段,发了论文顺利毕业:也经过三地辗转奔波来去的找工作,最终还是犹犹豫豫选择了自己知道以后可能会后悔的,果然就后悔了.所以还是应该选 ...
- LeetCode 3sum 问题
1. Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 259 [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 g ...
- [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]3Sum Closest
题目 Number: 16 Difficulty: Medium Tags: Array, Two Pointers Given an array S of n integers, find thre ...
最新文章
- 【C++】C++11 STL算法(二):修改序列的操作(Modifying sequence operations)
- 24本实体书包邮免费送!
- System Poles and Zeros 系统零点和极点
- py匹配字符串中间的字符串
- 第6次cplus实验
- python进行数据查询_如何进行python数据库查询?(实例解析)
- 前端学习(1034):jquery插件-全屏滚动
- 是自学前端还是培训学前端?
- mysqldump使用详解
- 数据库常用增删改查语句
- 银行数据仓库体系实践(15)--数据应用之巴塞尔新资本协议
- linux 同步北京时间_Linux系统同步系统时间为北京时间
- 基于Android studio新闻阅读客户端app
- Kafka同步发送与异步发送消息
- 实战 target 选择器,解放生产力
- 微信ios浏览器 与 iframe的不兼容问题
- 安装phpnow服务[Apache_pn]提示失败的解决方法
- MPLS之LDP协议
- ZTNA方案守住随时随地办公的安全防线
- UltraEdit| UltraEdit使用帮助
热门文章
- highroller赌城java下载,非常有用的链接
- mysql数据库主要负责存储_MySQL默认数据库简介
- 查找窗口隐藏了怎么办_如何快速查找网站管理页面
- 字典树-01字典树基础
- python duplicated函数_Python DataFrame使用drop_duplicates()函数去重(保留重复值,取重复值)...
- python 月活_TikTok用户月活超1亿意味着什么?什么是月活用户?
- 高并发 python socket send 异步_在Python中使用异步Socket编程性能测试
- html非零正实数正则表达式,正数|非负数|正整数正小数和0 |金额正则表达式
- 桔梗网导航怎么取消_货车从业资格证考试正式取消?交通部最新回复!
- python培训学习方法_python编程学习方法及技术进阶建议