LeetCode_3 sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = ? 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 = {- - -}, A solution set is:
(-, , )
(-, -, )
双指针的思想。使用DFS球排列组合时去重复的思想去重复
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> res;
sort(num.begin(), num.end());
int len = num.size();
if(len < ) return res;
for(int i = ; i <= len - ; ++i)
{
while(i> && i <= len - && num[i] == num[i-]) ++i;
if(i > len -) break;
int low = i + ;
int high = len -;
while(low < high){
int sum = num[i] + num[low] + num[high];
if(sum == ){
vector<int> tp;
tp.push_back(num[i]);
tp.push_back(num[low]);
tp.push_back(num[high]);
res.push_back(tp);
++low;
while(low < high && num[low] == num[low-])++low;
--high;
while(low <high && num[high] == num[high+]) --high;
}else if(sum < ){
++low;
while(low < high && num[low] == num[low-])++low;
}else{
--high;
while(low < high && num[high] == num[high+]) --high;
}
}
}
return res;
}
};
LeetCode_3 sum的更多相关文章
- LeetCode_3 sum closet
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- sybase 备份和恢复
use master go dump transaction MBFEWKDB with no_log go dump transaction MBFEHISDB with no_log go use ...
- Activity — 4 launch mode
launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的Activity实例,是否和其他Activity实例公用一个task ...
- [转]10款 Web 开发常备工具
文章地址:https://my.oschina.net/u/2903254/blog/798135 工欲善其事,必先利其器.如今 Web 开发标准越来越高,Web 开发者也在不断寻找途径提升自己的技能 ...
- Contest - 第10届“新秀杯”ACM程序设计大赛现场热身赛 赛后信息(题解)
Problem Id Title Problem A A+B Problem B 统计字数 Problem C 生日计算 Problem D 冬瓜的寒假之旅 Problem A(略 ...
- VMware migration to openstack kvm
- Stack & Heap in Java
Stack and Heap 都是Java用来在RAM中存放数据的地方.Java自动管理堆和栈,用户不能直接的设置堆或栈. Stack:存在于栈中的数据,其大小与生存周期是确定的,栈中的数据可以共享 ...
- Stack and queue.
队列的定义及基本运算 1.定义 队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表 (1)允许删除的一端称为队头(Front). (2)允许插入的一端称为队尾(Rea ...
- 在Java中兼容Windows和Linux的路径处理
Linux中的路径使用'/',而Windows下正好相反'\',Java提供了以系统属性的方式获取路径分隔符: System.getProperty("file.separator" ...
- ListView自定义滑动条
/** * 修改默认滑动条 */ private void SetSliderIcon() { try { Field f = AbsListView.class.getDeclaredField(& ...
- 微信企业号 出现redirect_uri unauthorized 50001 解决办法
在企业号内获得用户信息时,需要对域名授权,如果不授权会提示: redirect_uri unauthorized 50001 错误. 通常,我们会在 输入我们的授权域名. 今天在企业号内又新建了一个 ...