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
}
相关推荐
DICOM医学影像31 分钟前
2. go语言从零实现以太坊客户端-查询区块链账户余额
开发语言·golang·区块链·以太坊·web3.0·hardhat
练习时长一年40 分钟前
LeetCode热题100(杨辉三角)
算法·leetcode·职场和发展
栈与堆1 小时前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
努力学算法的蒟蒻2 小时前
day58(1.9)——leetcode面试经典150
算法·leetcode·面试
im_AMBER2 小时前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
西京刀客3 小时前
golang路由与框架选型(对比原生net/http、httprouter、Gin)
http·golang·gin
Mr -老鬼3 小时前
Rust与Go:从学习到实战的全方位对比
学习·golang·rust
踩坑记录3 小时前
leetcode hot100 560.和为 K 的子数组 medium 前缀和 + 哈希表
leetcode
独自破碎E4 小时前
【队列】按之字形顺序打印二叉树
leetcode
AlenTech4 小时前
206. 反转链表 - 力扣(LeetCode)
数据结构·leetcode·链表