485. Max Consecutive Ones

来源:互联网 发布:js 读取ios沙盒文件 编辑:程序博客网 时间:2024/06/07 02:26

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;}
0 0
原创粉丝点击