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

posted @ 2017-04-24 23:43  SapphireCastle  阅读(91)  评论(0编辑  收藏  举报