mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
16 lines
248 B
Go
16 lines
248 B
Go
package leetcode
|
|
|
|
func subarraySum(nums []int, k int) int {
|
|
count, pre := 0, 0
|
|
m := map[int]int{}
|
|
m[0] = 1
|
|
for i := 0; i < len(nums); i++ {
|
|
pre += nums[i]
|
|
if _, ok := m[pre-k]; ok {
|
|
count += m[pre-k]
|
|
}
|
|
m[pre] += 1
|
|
}
|
|
return count
|
|
}
|