【leetcode】3Sum
3Sum
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)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) { int n=num.size();
sort(num.begin(),num.end()); int i,j,k;
int a,b,c;
vector<vector<int> > result; for(i=;i<n-;i++)
{
j=i+;
k=n-; while(j<k)
{
a=num[i];
if(i>&&num[i]==num[i-])
{
break;
}
b=num[j];
if(j>i+&&num[j]==num[j-])
{
j++;
continue;
} c=num[k];
if(k<n-&&num[k]==num[k+])
{
k--;
continue;
} if(a+b+c==)
{
vector<int> tmp();
tmp[]=a;
tmp[]=b;
tmp[]=c;
result.push_back(tmp);
j++;
k--;
}
else if(a+b+c<)
{
j++;
}
else if(a+b+c>)
{
k--;
}
}
} return result; }
};
【leetcode】3Sum的更多相关文章
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【leetcode】3Sum (medium)
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(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】4Sum 解题报告
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- Ajax实现联动效果
用到了地区的数据库 html代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="s ...
- HashMap 和 HashTable区别
HashMap 非线程安全的 HashTable线程安全的 package Collections.Map; import java.util.HashMap; public class HashMa ...
- Java设计模式-组合模式(Composite)
组合模式有时又叫部分-整体模式在处理类似树形结构的问题时比较方便,看看关系图: 直接来看代码: public class TreeNode { private String name; private ...
- collections_python
代码 import collections#counter继承字典的方法,items(),keys(),vavle() obj = collections.Counter('acbdafcbad') ...
- BZOJ-3670 动物园 KMP+奇怪的东西
YveH爷再刷KMP,DCrusher看他刷KMP,跟着两个人一块刷KMP... 3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB ...
- [HNOI2010]BOUNCE 弹飞绵羊
题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...
- DEDECMS数据库执行原理、CMS代码层SQL注入防御思路
我们在上一篇文章中学习了DEDECMS的模板标签.模板解析原理,以及通过对模板核心类的Hook Patch来对模板的解析流量的攻击模式检测,达到修复模板类代码执行漏洞的目的 http://www.cn ...
- CMD修复
应该命令的路径被修改了. 试下在cmd下打入 path 命令看看.以下是正确的显示. PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\ ...
- Selenium2+python自动化13-Alert
不是所有的弹出框都叫alert,在使用alert方法前,先要识别出它到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决.alert\confirm\prompt弹出框 ...
- leach-matlab
http://www.oschina.net/code/snippet_860036_21044 clear;%清除变量 xm=100;%设置区域为100*100 ym=100; sink.x=0.5 ...