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, abc)
  • The solution set must not contain duplicate triplets.
 class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<int> NumCopy(num);
vector<vector<int> > returnvct;
sort(NumCopy.begin(),NumCopy.end());
int m=,cnt=;
while(NumCopy[m]<)
++m;
int f = m,j=m;
while(NumCopy[f] == ){
++f;
++cnt;
}
if(cnt>=){
f = m+cnt-;
j = m+cnt-;
}
for(int i=;f<NumCopy.size();++f){
int tempi =i,tempj =j;
int sum = - * (NumCopy[i]+NumCopy[j]);
while( sum > NumCopy[f]&& i<j ){
++i;
sum = -* (NumCopy[i]+ NumCopy[j]);
}
if(sum == NumCopy[f]){
vector<int> tmp;
tmp.push_back(i);
tmp.push_back(j);
tmp.push_back(f);
returnvct.push_back(tmp);
} while(sum < NumCopy[f] && i<j){
--j;
sum = - * (NumCopy[i]+ NumCopy[j]);
}
i = tempi;
j= tempj;
}
return returnvct;
}
};

利用昨天twoSum的方法 将a+b+c =0 变成 a+b =-c;数组 以0为界限 分为前后两部分。

可是又超时了::>_<:: 。

three Sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. 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 ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. 20161119微信小程序初识

    Tritonal ft. Angel Taylor - Getaway [Official Lyric Video]

  2. 如何在ASP.Net中实现RSA加密

    在我们实际运用中,加密是保证数据安全的重要手段.以前使用ASP时,对数据加密可以使用MD5和SHA1算法,这两种算法虽然快捷有效,但是无法对通过它们加密的密文进行反运算,即是解密.因此需要解密数据的场 ...

  3. table.appand(行数据) datagrid分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. Android开发总是难以入门

    发现自己很难入门,是真的太难,还是自己主观拒绝.

  5. [moka同学笔记]linux服务器防火墙的设置

    网站突然打不开:服务器停止了,重启后,防火墙自动启动,导致网站打不开. 1.查看防火墙 systemctl status firewalld 2.关闭防火墙 systemctl stop firewa ...

  6. 由SimpleAyncTaskExecutor到ListenableFutureTask

    Spring AsyncExecutor观后感 导语 本来想看下spring关于Async&Sync TaskExecutor的主要内容,看着看着发现ListenableTaskExecuto ...

  7. SharpGL学习笔记(十四) 材质:十二个材质球

    材质颜色 OpenGL用材料对光的红.绿.蓝三原色的反射率来近似定义材料的颜色.象光源一样,材料颜色也分成环境.漫反射和镜面反射成分,它们决定了材料对环境光.漫反射光和镜面反射光的反射程度.在进行光照 ...

  8. 【python】获取高德地图省市区县列表

    项目中需要用省市区来进行检索,原想高德地图肯定会有API来获得这些数据,结果没有找到,有一个接口好像可以用,但是会附带大量的边界坐标点. 所以就不如自己把高德的省市区列表扒下来,自己写接口来完成这个功 ...

  9. javascript作用域链学习笔记

    作用域链 "JavaScript中的函数运行在它们被定义的作用域里,而不是它们被执行的作用域里." --权威指南 在JavaScript中,一切皆对象,包括函数.函数对象和其它对象 ...

  10. LinearLayout嵌套

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...