zoukankan      html  css  js  c++  java
  • LeetCode_3Sum

    一.题目

    3Sum

      Total Accepted: 45112 Total Submissions: 267165My Submissions

    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)
    
    Show Tags
    Have you met this question in a real interview?  
    Yes
     
    No

    Discuss













    二.解题技巧

        这道题和另外一道题Two Sum非常类似,只是这道题是在数组中寻找三个数,使得其和为0,同一时候要求这三个数仅仅能出现一次。假设单纯得使用暴力算法来做的话。时间复杂度为O(n^3)。且非常难推断这一组数是否已经出现过。
        假设考虑将数组A(元素个数为n)进行升序排序。那么依照顺序将数组中的第i个数作为三个数中最小的数。寻找从A的第i+1个数到n-1个数中满足和为-A[i]的数。就能够找到满足三个数和为0的组合了,可是,单独考虑这样的情况,会出现反复的问题。

    要保证不出现反复的情况,当i!=0时,假设第i个数与第i-1个数同样的话,则不进行处理,直接处理第i+1个元素。这样。仅仅要保证三个数中最小的数是依照顺序递增的。那么算法找到的解就都是不反复的。

        这道题的边界条件在于:因为我们选择的是三个数中的最小值,因此。对于这个数的循环是[0, n-2)。同一时候,要对最小值进行消除反复的元素时。须要从第1个元素開始推断。假设从第0个元素開始推断的时候。可能会将0。0,0这样的情况忽略掉,因此,在消除反复的最小元素时,必须从第1个元素才開始。
        这道题在寻找数组A中的两个数的和为-A[i]时,能够考虑利用数组A是已经排序的条件,进行左右夹逼操作。在进行左右搜索的时候,须要将左右两边收缩到与当前元素不同的元素为止,这样做有两个原因:1.能够降低计算量;2.当当前的两个元素的和刚好等于-A[i]的时候,假设没有进行上面的缩放操作,那么就可能将反复的三个数保存下来,导致结果出错。



    三.实现代码

    class Solution
    {
    public:
        vector<vector<int> > threeSum(vector<int> &num)
        {
            int Size = num.size();
    
            vector<vector<int> > Result;
    
            if (Size < 3)
            {
                return Result;
            }
    
            sort(num.begin(), num.end());
    
            for (int Index_outter = 0; Index_outter < (Size - 2); Index_outter++)
            {
                int First = num[Index_outter];
                int Second = num[Index_outter + 1];
                const int Target = 0;
    
                if ((Index_outter != 0) && (First == num[Index_outter - 1]))
                {
                    continue;
                }
    
                int Start = Index_outter + 1;
                int End = Size - 1;
    
                while (Start < End)
                {
                    Second = num[Start];
                    int Third = num[End];
    
                    int Sum = First + Second + Third;
    
                    if (Sum == Target)
                    {
                        vector<int> Tmp;
                        Tmp.push_back(First);
                        Tmp.push_back(Second);
                        Tmp.push_back(Third);
    
                        Result.push_back(Tmp);
    
                        Start++;
                        End--;
                        while (num[Start] == num[Start - 1])
                        {
                            Start++;
                        }
                        while (num[End] == num[End + 1])
                        {
                            End--;
                        }
    
    
                    }
    
                    if (Sum < Target)
                    {
                        Start++;
                        while (num[Start] == num[Start -1])
                        {
                            Start++;
                        }
                    }
    
                    if (Sum > Target)
                    {
                        End--;
                        if (num[End] == num[End + 1])
                        {
                            End--;
                        }
                    }
    
                }
    
            }
        return Result;
    
        }
    };




    四.体会

        我自己的想法时,将数组进行排序,然后依照顺序选择数组A[1, n-1)中的元素作为三个数中的中位数来在剩下的已经排好序的数组中寻找满足条件的其它两个数。可是。选择中位数的这样的情况须要考虑的边界条件比較复杂,并没有选择最小值来得方便一些。同一时候,选择中位数之后。将已经排序的数组分成了两段,这样在进行收缩的时候。须要分别推断两个两边与i的关系,也比較easy出错。

        这道题通过对数组进行排序。从而依照顺序选择不同的最小值来避免出现反复的情况。这确实是一个比較好的编程技巧。



    版权全部,欢迎转载,转载请注明出处。谢谢微笑




  • 相关阅读:
    机器学习篇:sklearn.datasets
    Java篇:Java网络编程(二)网络地址及端口
    机器学习篇:sklearn.model_selection
    实践 12:多线程读写文件
    机器学习篇:循环神经网络RNN
    Java篇:Java网络编程(三)URL
    Java篇:Java网络编程(一)网络分层
    Java篇:Java的安全模型
    DataTable无法使用AsEnumerable ()的解决办法
    问一个关于WPF调用axWindowsMediaPlayer播放不稳定的问题,大侠们给个建议!谢了
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6812427.html
Copyright © 2011-2022 走看看