添加Go语言题解

This commit is contained in:
zouxinyao
2021-07-28 02:26:32 +08:00
parent 7eb8b4a9fd
commit 575b7612f0
52 changed files with 1679 additions and 104 deletions

View File

@@ -182,3 +182,24 @@ public:
}
};
```
Go Code:
```GO
func subarraySum(nums []int, k int) int {
m := map[int]int{}
// m存的是前缀和没有元素的时候和为0且有1个子数组(空数组)满足条件即m[0] = 1
m[0] = 1
sum := 0
cnt := 0
for _, num := range nums {
sum += num
if v, ok := m[sum - k]; ok {
cnt += v
}
// 更新满足前缀和的子数组数量
m[sum]++
}
return cnt
}
```