LeetCode - 15. 3Sum

简介: 15. 3Sum Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数列,找出这个数列中和为0的三元组.

 15. 3Sum

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;
}
目录
相关文章
|
11月前
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
|
12月前
|
机器学习/深度学习
leetcode - two-sum
leetcode - two-sum
65 0
|
机器学习/深度学习 Android开发 C++
LeetCode之Two Sum
LeetCode之Two Sum
99 0
|
算法 索引 C++
[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 unique triplets in the array which gives the sum of zero. Note: The solution set must not
1232 0
[LeetCode]--1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given n
1103 0