码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode——3Sum

时间:2015-09-18 19:00:30      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

原题如下:

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)

这道题让我想起了Leetcode的第一道题,即2Sum。很多人跟我说这两道题很像,但是我做完之后感觉一点都不像。

在2Sum这道题里面,Leetcode对时间的要求很高,所以用的是hash table的思想来解决的,即把value为key,position作为value,如果有冲突的话可以在一个bucket里放多个value。这样的话复杂度就只有O(n)。

3Sum不一样,是确定一个值之后来确定另外两个值。确定另外两个值复杂度最低也是O(n),所以总体的复杂度是O(n^n)。

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

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

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

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

Leetcode——3Sum

标签:

原文地址:http://my.oschina.net/u/1047616/blog/508215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!