LeetCode(15)题解--3Sum
https://leetcode.com/problems/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)
方法一:
两层循环+二分查找,复杂度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的更多相关文章
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
- [LeetCode][Python]15:3Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- [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 ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- ServletContext ActionContext ServletActionContext
1> ServletContext--------->SessionContext>RequestContext>PageContext 一个 WEB 运用程序只有一个 Ser ...
- MongoDb C# 驱动操作示例
c#操作mongo数据库 驱动采用http://www.oschina.net/p/mongo-csharp-driver C#驱动的基本数据库连接,增删改查操作 //定义对象 public clas ...
- 消防(bzoj 2282)
Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家 ...
- 学习javascript设计模式之发布-订阅(观察者)模式
1.发布-订阅模式又叫观察者模式,它定义对象之间一种一对多的依赖关系. 2.如何实现发布-订阅模式 2-1.首先指定好发布者 2-2.给发布者添加一个缓冲列表,用户存放回调函数以便通知订阅者 2-3. ...
- es6总结(四)--对象
- 【kindeditor】kindeditor的使用
官网:http://kindeditor.net/demo.php 效果:
- Perl语言入门--5--散列、hash
hash 一.关联数组的形式 %h=('a',1,'b',2); a是key 1是value b是key 2是value 以%开头,()为空散列 %h=('a'=>1,' ...
- linux下网络监控神器"iptraf-ng"
优点:监控的网络信息很全面,安装和使用方便 #centos安装: #yum 源使用centos自带的base源即可. yum install -y iptraf-ng #运行 iptraf-n ...
- 第1章 CentOS安装
一.安装说明 1.1 CentOS介绍 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux ...
- eclipse代码不能自动提示的问题解决
步骤如下:window->Preferences->Java->Editor->content assist 替换输入成: 普通:[.abcdefghijklmnopqrstu ...