leetcode 485:最大连续1的个数

题目:

算法思想:用一个变量记录连续1的个数,然后每次结束的时候找最大的。

代码:

    int findMaxConsecutiveOnes(vector<int>& nums) {
        int result = 0;
        int tmp = 0;
        for(int i = 0;i < nums.size();i++)
        {
            if(nums[i] == 1)
                tmp++;
            else
            {
                result = max(result,tmp);
                tmp = 0;
            }
        }
        result = max(result,tmp);
        return result;
    }
发布了137 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wh_computers/article/details/99710682