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
}
相关推荐
Chandler241 小时前
Go:方法
开发语言·c++·golang
小画家~7 小时前
第二十二: go与k8s、docker相关编写dockerfile
开发语言·golang·kubernetes
海风极客11 小时前
Go小技巧&易错点100例(二十五)
开发语言·后端·golang
二狗哈12 小时前
go游戏后端开发34:补杠功能与到时出牌
数据库·游戏·golang
余瑾瑜13 小时前
宝塔面板安装MySQL数据库并通过内网穿透工具实现公网远程访问
开发语言·后端·golang
梭七y13 小时前
【力扣hot100题】(089)最长有效括号
算法·leetcode·职场和发展
一只拉古15 小时前
掌握贪心(Greedy)算法:从 LeetCode 难题到系统架构
算法·leetcode·面试
阳洞洞17 小时前
leetcode 2787. Ways to Express an Integer as Sum of Powers
算法·leetcode·动态规划·01背包问题
阳洞洞17 小时前
leetcode 279. Perfect Squares
算法·leetcode·动态规划·完全背包问题
顾云澜17 小时前
Apache Superset本地部署结合内网穿透实现无公网IP远程查看数据
开发语言·后端·golang