LeetCode
来源:互联网 发布:中国人才流动数据 编辑:程序博客网 时间:2024/06/05 08:04
15. 3Sum Problem's Link
Problem's Link
----------------------------------------------------------------------------
Mean:
给定一个数列,找出这个数列中和为0的三元组.
analyse:
时间复杂度:O(n^2)
思路很简单,注意一下去重的方法.
Time complexity: O(N)
view code
/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-16-17.21
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
class Solution
{
public:
vector<vector<int> > threeSum(vector<int>& nums)
{
int si=nums.size();
vector<vector<int> > ret;
sort(nums.begin(),nums.end());
int target,sum,low,high;
for(int i=0;i<si;i++)
{
target=-nums[i];
low=i+1;
high=si-1;
// not exist two active integet's sum greater than target
if(target<0)
break;
while(low<high)
{
sum=nums[low]+nums[high];
if(sum<target)
++low;
else if(sum>target)
--high;
else
{
vector<int> triple(3,0);
triple[0]=nums[i];
triple[1]=nums[low];
triple[2]=nums[high];
ret.push_back(triple);
while(low<high && triple[1]==nums[low]) ++low;
while(low<high && triple[2]==nums[high]) --high;
}
}
while(i+1 < nums.size() && nums[i+1] == nums[i])
++i;
}
return ret;
}
};
int main()
{
Solution solution;
int n;
while(cin>>n)
{
vector<int> ve;
for(int i=0;i<n;++i)
{
int tmp;
cin>>tmp;
ve.push_back(tmp);
}
vector<vector<int> > ans;
ans=solution.threeSum(ve);
for(auto p1:ans)
{
for(auto p2:p1)
cout<<p2<<" ";
cout<<endl;
}
}
return 0;
}
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-02-16-17.21
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
class Solution
{
public:
vector<vector<int> > threeSum(vector<int>& nums)
{
int si=nums.size();
vector<vector<int> > ret;
sort(nums.begin(),nums.end());
int target,sum,low,high;
for(int i=0;i<si;i++)
{
target=-nums[i];
low=i+1;
high=si-1;
// not exist two active integet's sum greater than target
if(target<0)
break;
while(low<high)
{
sum=nums[low]+nums[high];
if(sum<target)
++low;
else if(sum>target)
--high;
else
{
vector<int> triple(3,0);
triple[0]=nums[i];
triple[1]=nums[low];
triple[2]=nums[high];
ret.push_back(triple);
while(low<high && triple[1]==nums[low]) ++low;
while(low<high && triple[2]==nums[high]) --high;
}
}
while(i+1 < nums.size() && nums[i+1] == nums[i])
++i;
}
return ret;
}
};
int main()
{
Solution solution;
int n;
while(cin>>n)
{
vector<int> ve;
for(int i=0;i<n;++i)
{
int tmp;
cin>>tmp;
ve.push_back(tmp);
}
vector<vector<int> > ans;
ans=solution.threeSum(ve);
for(auto p1:ans)
{
for(auto p2:p1)
cout<<p2<<" ";
cout<<endl;
}
}
return 0;
}
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- LeetCode
- 第一篇博客
- LeetCode
- LeetCode
- LeetCode
- LeetCode
- Vim快捷键操作命令大全
- 微信公众号开发(2)---接收订阅事件推送并回复消息
- C++
- ubuntu14.04安装ros系统
- 海思芯片.通用GPIO 使用篇
- sql server DML操作对索引的影响
- hbase rowkey设计
- 操作系统相关总结