[Swift]LeetCode560. 和爲K的子數組 | Subarray Sum Equals K

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-upapfpca-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.git

Example 1:github

Input:nums = [1,1,1], k = 2
Output: 2 

Note:數組

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

給定一個整數數組和一個整數 k,你須要找到該數組中和爲 k 的連續的子數組的個數。微信

示例 1 :app

輸入:nums = [1,1,1], k = 2
輸出: 2 , [1,1] 與 [1,1] 爲兩種不一樣的狀況。

說明 :spa

  1. 數組的長度爲 [1, 20,000]。
  2. 數組中元素的範圍是 [-1000, 1000] ,且整數 k 的範圍是 [-1e7, 1e7]。

56mscode

 1 class Solution {
 2     func subarraySum(_ nums: [Int], _ k: Int) -> Int {
 3                 if nums.count == 0 {
 4             return 0
 5         }
 6         var dic : Dictionary<Int, Int> = [:]
 7         var sum : Int = 0;
 8         var res : Int = 0;
 9         for i in 0 ..< nums.count {
10             sum += nums[i]
11             
12             if sum == k {
13                 res += 1
14             } 
15             
16             if dic[sum - k] != nil {
17                 res += dic[sum - k]!
18             }
19             
20             if dic[sum] == nil {
21                 dic[sum] = 1
22             } else {
23                 dic[sum] = dic[sum]! + 1
24             }
25         }        
26         return res
27     }
28 }

120mshtm

 1 class Solution {
 2     func subarraySum(_ nums: [Int], _ k: Int) -> Int {
 3         var sumCounter = [0: 1]
 4         var s = 0, res = 0
 5         for i in 0..<nums.count {
 6             s += nums[i]
 7             res += sumCounter[s - k] ?? 0
 8             sumCounter[s, default: 0] += 1
 9         }
10         return res
11     }
12 }

Runtime: 124 ms
Memory Usage: 19.8 MB
 1 class Solution {
 2     func subarraySum(_ nums: [Int], _ k: Int) -> Int {
 3         var res:Int = 0
 4         var sum:Int = 0
 5         var n:Int = nums.count
 6         var m:[Int:Int] = [0:1]
 7         for i in 0..<n
 8         {
 9             sum += nums[i]
10             res += m[sum - k,default:0]
11             m[sum,default:0] += 1
12         }
13         return res
14     }
15 }

124msblog

 1 class Solution {
 2     func subarraySum(_ nums: [Int], _ k: Int) -> Int {
 3       
 4       guard nums.count > 0 else { return -1 }
 5         var count = 0
 6         var sum = 0
 7         
 8         var dictionary: [Int: Int] = [:]
 9         dictionary[0] = 1
10         for i in 0..<nums.count {
11             sum += nums[i]
12             if let occurance = dictionary[sum - k] {
13                 count += occurance
14             }
15             
16             if let occurance = dictionary[sum] {
17                 dictionary[sum] = occurance + 1
18             } else {
19                 dictionary[sum] = 1
20             }
21             
22         }
23         
24         return count
25     }
26 }

148ms

 1 class Solution {
 2     func subarraySum(_ nums: [Int], _ k: Int) -> Int {
 3         var partialSums = [Int: [Int]]()
 4         partialSums[0] = [0]
 5         var found = [(Int, Int)]()
 6         var sum = 0
 7         for (i, num) in nums.enumerated() {
 8             sum += num
 9             if let bounds = partialSums[sum - k] {
10                 bounds.forEach { found.append(($0, i)) }
11             }
12             partialSums[sum, default: []].append(i)
13         }
14         return found.count
15     }
16 }
相關文章
相關標籤/搜索