Leetcode notes: 4Sum, leetcode notes 4sum

Source: Internet
Author: User

Leetcode notes: 4Sum, leetcode notes 4sum

I. Description

Ii. problem-solving skills

On the surface, this question is very similar to 3Sum. In fact, we can use the same thinking and method. However, in this case, the time complexity is O (n ^ 3 ), the space complexity is O (1) and will time out.

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 ).

Iii. instance 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. Experience

This question is an extension of 3Sum, but it needs to be converted into an idea to reduce the computational complexity. If we only follow the extension of 3Sum to solve this problem, the space complexity of the algorithm will reach O (n ^ 3), but 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.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.