Leetcode之Max Consecutive Ones 问题

来源:互联网 发布:java开发网上商城步骤 编辑:程序博客网 时间:2024/06/05 09:56

问题描述:

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

示例:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.

Note:

1.The input array will only contain 0 and 1.

2.The length of input array is a positive integer and will not exceed 10,000

问题来源:Max Consecutive Ones (详细地址:https://leetcode.com/problems/max-consecutive-ones/description/)

思路分析:这道题很简单吧,也没啥技巧,唯一可以注意的就是代码简洁的部分了,题目说的很清楚了,数组中只有0和1,遇到1的时候就累加,更新一下最大值,遇到0的时候计数器就重置为0

代码: