leetcode筆記:4Sum,leetcode筆記4sum

來源:互聯網
上載者:User

leetcode筆記:4Sum,leetcode筆記4sum

一、題目描述

二、解題技巧

這道題從表面上看與3Sum極其相似,事實上確實可以使用相同的思維和方法,只不過這樣做的話,時間複雜度為O(n^3),空間複雜度為O(1)將逾時。

這道題也可以在排序之後先計算後面兩個數的和,將其方法一個雜湊表中,由於可能存在不同的兩個數的和為相同值,因此,可以考慮將和為相同的值放在一個鏈表中,然後將變數頭放在雜湊表中。然後再按照3Sum的思路,不過第三個數在這裡變成了第三個和第四個數的和,通過雜湊表可以方便地找到和為固定值的數的鏈表,就可以找到合格四個數。這種方法的時間複雜度為O(n^2),空間複雜度也為O(n^2)。

三、執行個體代碼

    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;          }      };  

四、體會

這道題是3Sum的一種延伸,但需要轉化一種思路以降低計算複雜度。如果僅僅按照3Sum的延伸來做這道題的話,演算法的空間複雜度會達到O(n^3),但是空間複雜度為O(1)。可以換一種思路,在排序之後,先計算後面兩個數的和,並用雜湊表儲存起來,然後就將問題轉變為了3Ssum的問題,只不過計算出第三個數之後,還需要在雜湊表中找到和滿足條件的第三個數和第四個數。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.