Leetcode Note: Go - Max Consecutive Ones

Max Consecutive Ones - LeetCode
https://leetcode.com/problems/max-consecutive-ones/

  • Go 言語で取り組んだメモ

所感

  • int 配列の要素で 1 が連続した最大回数を return する

回答

Go: simple solution - LeetCode Discuss
https://leetcode.com/problems/max-consecutive-ones/discuss/552742/Go%3A-simple-solution

func findMaxConsecutiveOnes(nums []int) int {
    res, tmp := 0, 0
    for i := range nums {
        if nums[i] == 1 {
            tmp++
        } else {
            res = max(res, tmp)
            tmp = 0
        }
    }
    return max(res, tmp)
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}