自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

LeetCode 之 四數(shù)之和

開發(fā) 前端
給你一個(gè)由 n 個(gè)整數(shù)組成的數(shù)組 nums,和一個(gè)目標(biāo)值 target 。請你找出并返回滿足下述全部條件且不重復(fù)的四元組 [nums[a], nums[b], nums[c], nums[d]](若兩個(gè)四元組元素一一對(duì)應(yīng),則認(rèn)為兩個(gè)四元組重復(fù)).

[[443184]]

前言

我們社區(qū)陸續(xù)會(huì)將顧毅(Netflix 增長黑客,《iOS 面試之道》作者,ACE 職業(yè)健身教練。微博:@故胤道長[1])的 Swift 算法題題解整理為文字版以方便大家學(xué)習(xí)與閱讀。

LeetCode 算法到目前我們已經(jīng)更新了 17 期,我們會(huì)保持更新時(shí)間和進(jìn)度(周一、周三、周五早上 9:00 發(fā)布),每期的內(nèi)容不多,我們希望大家可以在上班路上閱讀,長久積累會(huì)有很大提升。

不積跬步,無以至千里;不積小流,無以成江海,Swift社區(qū) 伴你前行。如果大家有建議和意見歡迎在文末留言,我們會(huì)盡力滿足大家的需求。

難度水平:中等

1. 描述

給你一個(gè)由 n 個(gè)整數(shù)組成的數(shù)組 nums,和一個(gè)目標(biāo)值 target 。請你找出并返回滿足下述全部條件且不重復(fù)的四元組 [nums[a], nums[b], nums[c], nums[d]](若兩個(gè)四元組元素一一對(duì)應(yīng),則認(rèn)為兩個(gè)四元組重復(fù)):

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意順序 返回答案。

2. 示例

示例 1

  1. 輸入:nums = [1,0,-1,0,-2,2], target = 0 
  2. 輸出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] 

示例 2

  1. 輸入:nums = [2,2,2,2,2], target = 8 
  2. 輸出:[[2,2,2,2]] 

約束條件:

  • 1 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109

3. 答案

  1. class FourSum { 
  2.     func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { 
  3.         let nums = nums.sorted(by: <) 
  4.         var threeSum = 0 
  5.         var twoSum = 0 
  6.         var left = 0 
  7.         var right = 0 
  8.         var res = [[Int]]() 
  9.          
  10.         guard nums.count >= 4 else { 
  11.             return res 
  12.         } 
  13.          
  14.         for i in 0..<nums.count - 3 { 
  15.             guard i == 0 || nums[i] != nums[i - 1] else { 
  16.                 continue 
  17.             } 
  18.             threeSum = target - nums[i] 
  19.              
  20.             for j in i + 1..<nums.count - 2 { 
  21.                 guard j == i + 1 || nums[j] != nums[j - 1] else { 
  22.                     continue 
  23.                 } 
  24.                 twoSum = threeSum - nums[j] 
  25.                  
  26.                 left = j + 1 
  27.                 right = nums.count - 1 
  28.                 while left < right { 
  29.                     if nums[left] + nums[right] == twoSum { 
  30.                         res.append([nums[i], nums[j], nums[left], nums[right]]) 
  31.                         repeat { 
  32.                             left += 1 
  33.                         } while left < right && nums[left] == nums[left - 1] 
  34.                             repeat { 
  35.                                 right -= 1 
  36.                         } while left < right && nums[right] == nums[right + 1] 
  37.                     } else if nums[left] + nums[right] < twoSum { 
  38.                         repeat { 
  39.                             left += 1 
  40.                         } while left < right && nums[left] == nums[left - 1] 
  41.                     } else { 
  42.                         repeat { 
  43.                             right -= 1 
  44.                         } while left < right && nums[right] == nums[right + 1] 
  45.                     } 
  46.                 } 
  47.             } 
  48.         } 
  49.                  
  50.         return res 
  51.     } 

  • 主要思想:對(duì)數(shù)組進(jìn)行排序并遍歷,根據(jù)它們的和大于或小于等于目標(biāo),向左遞增或向右遞減
  • 時(shí)間復(fù)雜度:O(n^3)
  • 空間復(fù)雜度:O(nC4)

該算法題解的倉庫:LeetCode-Swift[2]

點(diǎn)擊前往 LeetCode[3] 練習(xí)

參考資料

[1]@故胤道長:

https://m.weibo.cn/u/1827884772

[2]LeetCode-Swift:

https://github.com/soapyigu/LeetCode-Swift

[3]LeetCode:

https://leetcode.com/problems/4sum

【編輯推薦】

 

 

責(zé)任編輯:姜華 來源: Swift社區(qū)
相關(guān)推薦

2021-12-24 09:01:05

LeetCode三數(shù)之和算法

2022-02-15 08:25:22

hash表快排二分查找

2021-01-21 08:23:29

鏈表單鏈表循環(huán)鏈表

2021-03-12 08:19:20

數(shù)組跳躍游戲

2021-12-01 09:00:57

LeetCode回文數(shù)字算法

2020-10-12 11:16:32

數(shù)組特定值元素

2022-02-11 09:01:45

LeetCode函數(shù)括號(hào)生成

2017-02-06 10:56:37

大數(shù)據(jù)現(xiàn)場設(shè)備數(shù)治

2021-11-24 09:08:38

LeetCode字符串算法

2021-12-14 09:01:01

LeetCode整數(shù)羅馬數(shù)字

2021-01-14 08:23:15

LeetCode變量

2021-12-15 09:00:53

LeetCode 羅馬數(shù)字整數(shù)

2017-06-15 13:29:12

AkkaSpark異步

2022-03-01 17:16:16

數(shù)倉建模ID Mapping

2022-02-18 09:02:04

數(shù)據(jù)倉庫治理

2021-02-04 08:18:53

LeetCode鏈表

2021-01-22 08:30:50

LeetCode數(shù)字數(shù)組

2016-12-09 09:23:50

android組件Service

2022-08-22 17:46:56

虛擬數(shù)倉Impala

2023-01-05 08:45:47

算法Java
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)