8. 3Sum FROM Leetcode

题目

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 contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

频度: 5

解题之法

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& num) {

        vector<vector<int> > res;

        std::sort(num.begin(), num.end());

        for (int i = 0; i < num.size(); i++) {
       
            int target = - num[i];
            int front = i + 1;
            int back = num.size() - 1;

             while (front < back) {

                 int sum = num[front] + num[back];
            
                 // Finding answer which start from number num[i]
                if (sum < target)
                    front++;
                else if (sum > target)
                    back--;
                else {
                    vector<int> triplet(3, 0);
                    triplet[0] = num[i];
                    triplet[1] = num[front];
                    triplet[2] = num[back];
                    res.push_back(triplet);
                
                    // Processing duplicates of Number 2
                    // Rolling the front pointer to the next different number forwards
                    while (front < back && num[front] == triplet[1]) front++;

                    // Processing duplicates of Number 3
                    // Rolling the back pointer to the next different number backwards
                    while (front < back && num[back] == triplet[2]) back--;
            }
       }

        // Processing duplicates of Number 1
        while (i + 1 < num.size() && num[i + 1] == num[i]) 
            i++;
    }
    return res;
    }
};

分析

the key idea is the same as the TwoSum problem. When we fix the 1st number, the 2nd and 3rd number can be found following the same reasoning as TwoSum.

The only difference is that, the TwoSum problem of LEETCODE has a unique solution. However, in ThreeSum, we have multiple duplicate solutions that can be found. Most of the OLE errors happened here because you could've ended up with a solution with so many duplicates.

The naive solution for the duplicates will be using the STL methods like below :

std::sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());

But according to my submissions, this way will cause you double your time consuming almostly.

A better approach is that, to jump over the number which has been scanned, no matter it is part of some solution or not.

If the three numbers formed a solution, we can safely ignore all the duplicates of them.

We can do this to all the three numbers such that we can remove the duplicates.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,011评论 0 23
  • 所有人都是受害着
    尘埃之后阅读 239评论 0 0
  • 县城虽不大,但司法还是存在的,而且消息,也流传的非常快。只不过片刻功夫,官府就有人来了。 来的人有当地小有名气的捕...
    Hugh1029阅读 244评论 0 0
  • 幸福感是目前很多人所追求的一种「虚幻的感觉」,它无非就是生活更加舒适时的那种惬意感。当然,按照我的理解,这种幸福感...
    永恒在天阅读 696评论 0 1
  • 推荐语:以廉价者的标准要求自己,你便是廉价者;以高贵者的标准要求自己,你便是高贵者。有些人即便在他们最穷困潦倒的时...
    鸡毛大叔阅读 393评论 0 0

友情链接更多精彩内容