LeetCode 525. 连续数组
题目描述
思路分析
前缀和
参考代码
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
func findMaxLength(nums []int) int {
prefixSum := make(map[int]int)
sum := 0
maxLen := 0
prefixSum[0] = -1
for i, num := range nums {
if num == 0 {
sum -= 1
} else {
sum += 1
}
if index, ok := prefixSum[sum]; ok {
maxLen = max(maxLen, i-index)
} else {
prefixSum[sum] = i
}
}
return maxLen
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
1
write your code here
1
write your code here
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用