1. Title
485. Max Consecutive Ones
2. Http address
https://leetcode.com/problems/max-consecutive-ones/?tab=Description
3. The question
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
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:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
4. code
public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int maxLen = 0; if (nums == null || nums.length <= 0) { return maxLen; } int len = nums.length; int curLen = 0; for (int i = 0; i < len; i++) { if (nums[i] == 1) { curLen++; } else { maxLen = Math.max(maxLen, curLen); curLen = 0; } } return Math.max(maxLen, curLen); } }