-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
39 lines (31 loc) · 839 Bytes
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
560. Subarray Sum Equals K
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
Time complexity: O(n)
Space complexity: O(n)
*/
class Solution {
func subarraySum(_ nums: [Int], _ k: Int) -> Int {
// [1, 2, 3, 2, -2] k = 5
// [1, 3, 6, 8, 6]
//
//
//
var map = [Int: Int]()
map[0] = 1
var sum = 0
var total = 0
for num in nums {
sum += num
if let occurences = map[sum - k] {
total += occurences
}
if map[sum] != nil {
map[sum]! += 1
} else {
map[sum] = 1
}
}
return total
}
}