485. Max Consecutive Ones - 最大连续个数

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

分析

也就是找一个二进制数组中最大的连续1的个数,简单点就遍历统计就可以了。

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;
}

猜你喜欢

转载自blog.csdn.net/qq_25077833/article/details/61559678
今日推荐