485. Max Consecutive Ones

Question:

Given a binary array, find the maximum number of consecutive 1s in this array.

Solution:

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int gap = 0;
        int max = 0;
        int i = 0;
        
        while (i < nums.size()) {
            if (nums[i]) {
                gap ++;
            } else {
                gap = 0;
            }
            if (gap > max) {
                max = gap;
            }
            i ++;
        }

        return max;
    }
};

题目直达:https://leetcode.com/problems/max-consecutive-ones/#/description

原文地址:https://www.cnblogs.com/SapphireCastle/p/6759813.html