LeetCode_4Sum, leetcode

Source: Internet
Author: User

LeetCode_4Sum, leetcode
I. Question 4Sum Total Accepted: 29675 Total Submissions: 138870My Submissions

Given an array S of n integers, are there elements a, B, c, and d in S such that a + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a, B, c, d) must be in non-descending order. (ie, a ≤ B ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.

    For example, given array S = {1 0 -1 0 -2 2}, and target = 0.    A solution set is:    (-1,  0, 0, 1)    (-2, -1, 1, 2)    (-2,  0, 0, 2)

Show TagsHave you met this question in a real interview? Yes No

Discuss













Ii. problem-solving skillsThis question can adopt the same idea as 3Sum, but the time complexity is O (n ^ 3) and the space complexity is O (1 ). This question can also be used to calculate the sum of the next two numbers after sorting. In a hash table, the sum of the two numbers may be the same, you can consider placing the same sum in a linked list, and then put the variable header in the hash table. Then follow the 3Sum principle. However, the 3rd number is the sum of the 3rd and 4th numbers. You can use a hash table to conveniently find the linked list of the numbers for fixed values, you can find the four numbers that meet the conditions. The time complexity of this method is O (n ^ 2), and the space complexity is O (n ^ 2 ). (I have not implemented the second method)

Iii. Implementation Code
class Solution{public:    vector<vector<int> > fourSum(vector<int> &num, int target)    {        vector<vector<int> > Result;        int Size = num.size();        if (Size < 4)        {            return Result;        }        // sort the array        sort(num.begin(), num.end());        for (int Index_first = 0; Index_first < (Size - 3); Index_first++)        {            int First = num[Index_first];            if ((Index_first != 0) && (num[Index_first - 1] == num[Index_first]))            {                continue;            }            for (int Index_second = Index_first + 1; Index_second < (Size - 2); Index_second++)            {                if ((Index_second != (Index_first + 1)) && (num[Index_second - 1] == num[Index_second]))                {                    continue;                }                int Second = num[Index_second];                int Index_third = Index_second + 1;                int Index_foud = Size - 1;                while (Index_third < Index_foud)                {                    int Third = num[Index_third];                    int Fourd = num[Index_foud];                    int Sum = First + Second + Third + Fourd;                    if (Sum == target)                    {                        vector<int> Tmp;                        Tmp.push_back(First);                        Tmp.push_back(Second);                        Tmp.push_back(Third);                        Tmp.push_back(Fourd);                        Result.push_back(Tmp);                        Index_third++;                        while ((Index_third <= (Size - 1)) && (num[Index_third] == num[Index_third - 1]))                        {                            Index_third++;                        }                        Index_foud--;                        while ((Index_foud > Index_second) && (num[Index_foud] == num[Index_foud + 1]))                        {                            Index_foud--;                        }                    }                    if (Sum < target)                    {                        Index_third++;                        while ((Index_third < Size) && (num[Index_third] == num[Index_third - 1]))                        {                            Index_third++;                        }                    }                    if (Sum > target)                    {                        Index_foud--;                        while ((Index_foud > Index_second) && (num[Index_foud] == num[Index_foud + 1]))                        {                            Index_foud--;                        }                    }                }            }        }        return Result;    }};





Iv. ExperienceThis question is an extension of 3Sum. However, if we only follow the extension of 3Sum, the space complexity of the algorithm will reach O (n ^ 3 ), however, the space complexity is O (1 ). Another way of thinking is to calculate the sum of the next two numbers after sorting, store them in a hash table, and then change the problem to 3Ssum, after calculating the third number, you must find the third number and the fourth number that meet the conditions in the hash table. The conversion of one idea can greatly reduce the computing complexity, which is the pleasure of thinking algorithms.


All rights reserved. You are welcome to reprint it. Please indicate the source for reprinting. Thank you.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.