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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章