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
}
相关推荐
阿昭L26 分钟前
leetcode链表是否有环
算法·leetcode·链表
yaoh.wang34 分钟前
力扣(LeetCode) 83: 删除排序链表中的重复元素 - 解法思路
程序人生·算法·leetcode·链表·面试·职场和发展
阿昭L39 分钟前
leetcode旋转链表
算法·leetcode·链表
im_AMBER40 分钟前
Leetcode 81 【滑动窗口(定长)】
数据结构·笔记·学习·算法·leetcode
Swift社区1 小时前
LeetCode 452 - 用最少数量的箭引爆气球
算法·leetcode·职场和发展
yaoh.wang1 小时前
力扣(LeetCode) 70: 爬楼梯 - 解法思路
python·算法·leetcode·面试·职场和发展·动态规划·递归
Learner__Q1 小时前
每天五分钟:二分查找-LeetCode高频题解析_day4
python·算法·leetcode
moxiaoran57532 小时前
Go语言的递归函数
开发语言·后端·golang
朝花不迟暮2 小时前
Go基础-闭包
android·开发语言·golang
iAkuya2 小时前
(leetcode)力扣100 18矩阵置零(哈希)
leetcode·矩阵·哈希算法