Leetcode——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.spa

Note:
.net

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)code

  • The solution set must not contain duplicate triplets.orm

    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

这道题让我想起了Leetcode的第一道题,即2Sum。不少人跟我说这两道题很像,可是我作完以后感受一点都不像。blog

在2Sum这道题里面,Leetcode对时间的要求很高,因此用的是hash table的思想来解决的,即把value为key,position做为value,若是有冲突的话能够在一个bucket里放多个value。这样的话复杂度就只有O(n)。排序

3Sum不同,是肯定一个值以后来肯定另外两个值。肯定另外两个值复杂度最低也是O(n),因此整体的复杂度是O(n^n)。three

仔细观察题目,第一个要求是要将数组以增序输出,因此咱们最好将整个数组排序,复杂度为O(n^n),最低为O(nlogn)(使用快排)。排序对后面的扫描是至关有用的。ip

第二个要求是不能重复。初看上去挺难的,其实很简单,只须要在扫描的过程当中跳太重复的既可。element

代码以下,思路比较简单:

class Solution
{
public:
    vector< vector<int> > threeSum(vector<int>& nums)
    {
        vector< vector<int> > result;
        result.clear();
        unsigned int i;
        unsigned int j;
        unsigned int k;
        int sum;
        if (nums.size() < 3)
        {
            return result;
        }

        bubble_sort(nums);  //首先进行从小到大排序
        i = 0;
        while (i < nums.size() - 2)
        {
            j = i + 1;
            k = nums.size() - 1;
            while (j < k)
            {
                sum = nums[i] + nums[j] + nums[k];
                if (0 == sum)   //和为0时,须要去除那些重复的数字
                {
                    vector<int> triplet;
                    triplet.push_back(nums[i]);
                    triplet.push_back(nums[j]);
                    triplet.push_back(nums[k]);
                    result.push_back(triplet);
                    //去除重复
                    while (j < k && nums[j] == triplet[1]) j++;     //去重又自增,这个循环总会执行至少一遍
                    while (j < k && nums[k] == triplet[2]) k--;
                }
                else if (sum > 0)   //和大于0,k必须向左移动下降组合的值
                {
                    k--;
                }
                else
                {
                    j++;
                }
            }

            int temp = nums[i];
            while (i < nums.size() - 2 && nums[i] == temp) i++;
        }
        return result;
    }

private:
    void bubble_sort(vector<int>& nums) //冒泡排序
    {
        bool is_swap;

        for (unsigned int i = nums.size() - 1; i > 0; i--)
        {
            is_swap = false;
            for (unsigned int j = 0; j < i; j++)
            {
                if (nums[j] > nums[j+1])
                {
                    swap(nums[j], nums[j+1]);
                    is_swap = true;
                }
            }
            if (false == is_swap)
            {
                break;
            }
        }
    }

    void swap(int& a, int& b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
};

参考文章:

http://blog.csdn.net/zhouworld16/article/details/16917071

http://blog.csdn.net/nanjunxiao/article/details/12524405

相关文章
相关标签/搜索