Golang | Leetcode Golang题解之第525题连续数组

题目:

题解:

Go 复制代码
func findMaxLength(nums []int) (maxLength int) {
    mp := map[int]int{0: -1}
    counter := 0
    for i, num := range nums {
        if num == 1 {
            counter++
        } else {
            counter--
        }
        if prevIndex, has := mp[counter]; has {
            maxLength = max(maxLength, i-prevIndex)
        } else {
            mp[counter] = i
        }
    }
    return
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}
相关推荐
py有趣1 天前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗1 天前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
吃着火锅x唱着歌1 天前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
十八岁讨厌编程1 天前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
钢门狂鸭1 天前
go开发规范指引
开发语言·驱动开发·golang
小当家.1051 天前
[LeetCode]Hot100系列.贪心总结+思想总结
算法·leetcode·职场和发展
脚踏实地的大梦想家1 天前
【Go】P19 Go语言并发编程核心(三):从 Channel 安全到互斥锁
开发语言·安全·golang
im_AMBER1 天前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
Tony Bai1 天前
Go GUI 开发的“绝境”与“破局”:2025 年现状与展望
开发语言·后端·golang
豆浆whisky1 天前
Go分布式追踪实战:从理论到OpenTelemetry集成|Go语言进阶(15)
开发语言·分布式·golang