Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

485. 最大连续1的个数 #31

Open
pigpigever opened this issue Dec 19, 2019 · 0 comments
Open

485. 最大连续1的个数 #31

pigpigever opened this issue Dec 19, 2019 · 0 comments
Labels
数组 数组类型的算法题

Comments

@pigpigever
Copy link
Owner

题目剖析

给定一个二进制数组, 计算其中最大连续1的个数。

解题思路

用一个变量 count 计数,用一个变量 res 记录结果

示例代码

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxConsecutiveOnes = function(nums) {
    let count = 0, res = 0
    for (const num of nums) {
        if (num === 1) {
            count++
        } else {
            count = 0
        }
        res = Math.max(res, count)
    }
    return res
};
@pigpigever pigpigever added the 数组 数组类型的算法题 label Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
数组 数组类型的算法题
Projects
None yet
Development

No branches or pull requests

1 participant