485. Max Consecutive Ones - Max Consecutive Ones

https://leetcode.com/problems/max-consecutive-ones

analyze

That is, to find the largest number of consecutive 1s in a binary array, simply traverse the statistics.

int findMaxConsecutiveOnes(int* nums, int numsSize) {
    int i = 0;
    int maxLen = 0;
    int tmpLen = 0;

    for (i = 0; i < numsSize; i++)
    {
        if (nums[i] == 1)
        {
            tmpLen++;
            if (tmpLen > maxLen)
            {
                maxLen = tmpLen;
            }
        }
        else
        {
            tmpLen = 0;
        }
    }

    return maxLen;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325482207&siteId=291194637