485. Max Consecutive Ones
Leetcode 問題: 485. Max Consecutive Ones
		
		Categories:
題目
Given a binary array
nums, return the maximum number of consecutive 1’s in the array.
傳入一個整數陣列 nums,回傳連續出現 1 的次數是多少
Example
Input: nums = [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
Input: nums = [1,0,1,1,0,1]
Output: 2
演算法
1. 找尋 1
當出現 1,則計數器 +1,然後紀錄目前抓到的計數器最大值
2. 不是 1
重設整個計數器,重新尋找
答案
JavaScript
function findMaxConsecutiveOnes(numsList) {
    // 最多連續出現 1 次數
    var maxConsecutiveOneCount = 0;
    // 目前 1 出現次數
    var currentOneCount = 0;
    for (var i = 0; i < numsList.length; i++) {
        // 目前的數字
        var currentNumber = numsList[i];
        if (currentNumber == 1) {
            // 如果目前數字是 1,計算 1 出現次數
            currentOneCount++;
            // 比較「最多連續出現 1 次數」與「目前 1 出現次數」取最大值
            maxConsecutiveOneCount = Math.max(maxConsecutiveOneCount, currentOneCount);
        }
        else {
            // 如果目前數字不是 1,重新計算 1 出現次數
            currentOneCount = 0;
        }
    }
    return maxConsecutiveOneCount;
}
TypeScript
function findMaxConsecutiveOnes(numsList: number[]): number {
    // 最多連續出現 1 次數
    let maxConsecutiveOneCount = 0;
    // 目前 1 出現次數
    let currentOneCount = 0;
    for (let i = 0; i < numsList.length; i++) {
        // 目前的數字
        let currentNumber = numsList[i];
        if (currentNumber == 1) {
            // 如果目前數字是 1,計算 1 出現次數
            currentOneCount++;
            // 比較「最多連續出現 1 次數」與「目前 1 出現次數」取最大值
            maxConsecutiveOneCount = Math.max(maxConsecutiveOneCount, currentOneCount);
        } else {
            // 如果目前數字不是 1,重新計算 1 出現次數
            currentOneCount = 0
        }
    }
    return maxConsecutiveOneCount;
};