https://leetcode.com/problems/3sum/

题目:

Given an array S of n integers, are there elements abc 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)

方法一:

两层循环+二分查找,复杂度O(n^2 logn). 太慢了

 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
set<vector<int>> res;
vector<vector<int>> output;
vector<int> sol;
sort(nums.begin(),nums.end()); //sort first
int i,j,k,t,x,n=nums.size();
for(i=;i<n;i++){
for(j=i+;j<n-;j++){
t=nums[i]+nums[j];
x=findSol(-t,nums,j+,n-);
if(x!=-){
sol.clear();
sol.push_back(nums[i]);
sol.push_back(nums[j]);
sol.push_back(nums[x]);
res.insert(sol);
}
}
}
set<vector<int>> :: iterator iter;
for(iter=res.begin();iter!=res.end();iter++){
output.push_back(*iter);
}
return output;
}
int findSol(int target,vector<int> nums,int begin,int end){
if(nums[begin]==target)
return begin;
if(nums[end]==target)
return end;
if(nums[begin]>target||nums[end]<target)
return -;
int mid;
while(begin<=end){
mid=(begin+end)/;
if(nums[mid]==target)
return mid;
else if(nums[mid]>target){
end=mid-;
}
else
begin=mid+;
}
return -;
}
};

方法二:  一层循环加two sum思想(https://leetcode.com/problems/two-sum/),O(n^2).

 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
int i,t,a,b,k,n=nums.size();
for(i=;i<n-;i++){
t=-nums[i];
a=i+;
b=n-;
while(a<b){
k=nums[a]+nums[b];
if(k<t){
a++;
}
else if(k>t){
b--;
}
else{
vector<int> sol(,);
sol[]=nums[i];
sol[]=nums[a];
sol[]=nums[b];
res.push_back(sol);
while (a < b && nums[a] == sol[])
a++;
while (a < b && nums[b] == sol[])
b--;
}
}
while (i + < nums.size() && nums[i + ] == nums[i])
i++;
} //deduplicate, but it is so slow!
//sort(res.begin(), res.end());
//res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
};

LeetCode(15)题解--3Sum的更多相关文章

  1. LeetCode(16)题解--3Sum Closest

    https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...

  2. [LeetCode][Python]15:3Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...

  3. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  4. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  5. [LeetCode] 15. 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 ...

  6. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  7. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. 关于sql语句实现将'1,2,3'转1,2,3等竖横转换问题

    这是本人的第一个博客,以后会及时整理遇到的问题,方便和大家进行交流. 第一次也不知道说什么了,直接进入正题吧. 因为我的数据在设计时候数据源是竖列存的,满足条件的数据是横列存的.如下图所示: 我要筛选 ...

  2. springboot中的controller注解没有生效

    springboot中的controller注解没有生效  , 启动的Application类没有在controller的父目录或同级目录

  3. jquery 日期插件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 应用node+express+mysql 实现简单的增删改查

    记录下来备忘 1.准备好webstrom编辑器,编辑器调整字体大小等在file->settings->editor下设置 注册码 来源网上: 2017.2.27更新 选择“license ...

  5. 《Programming in C》读书笔记

    该书由美国Seephen G.Kochan著 贾洪峰译,电子工业出版社,来源是九江学院图书馆采购,现在藏于九江学院图书馆逸夫楼. 本书的主要内容: 第一章.基础知识 第二章.编译和运行第一个程序 第三 ...

  6. 对于Redux的理解

    在移动端项目,经常会在不同view中进行传递数据,事件.当事件比较少时,我们可以通过常规的事件流方法,注册,发布事件 进行响应等等.但是项目中一个事件多处响应时候,就会使程序变得相当复杂.在现在的Vu ...

  7. 洛谷——P2386 放苹果

    P2386 放苹果 题目背景 (poj1664) 题目描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发(5,1,1和1,1,5是同一种方法) 输入输出格式 输入 ...

  8. Java多线程中Lock的使用

    Jdk1.5以后,在java.util.concurrent.locks包下,有一组实现线程同步的接口和类,说到线程的同步,可能大家都会想到synchronized关键字, 这是java内置的关键字, ...

  9. 12.Java web--过滤器与监听器

    1)过滤器 就是为请求与目标之间加一个或多个过滤器 自定义过滤器要实现Filter接口 下面是定义一个所有Servlet的请求中文不乱码 /** * 用于servlet输出中文乱码的过滤 */ @We ...

  10. 自定义PropertyGrid控件【转】

    自定义PropertyGrid控件 这篇随笔其实是从别人博客上载录的.感觉很有价值,整理了一下放在了我自己的博客上,希望原作者不要介意. 可自定义PropertyGrid控件的属性.也可将属性名称显示 ...