485. Max Consecutive Ones
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
and1
. - The length of input array is a positive integer and will not exceed 10,000
找出一个二进制串中连续的1的个数。
1 public int findMaxConsecutiveOnes(int[] nums) { 2 int max=0,i=0; 3 while (i<nums.length) 4 { 5 int sum=0; 6 while (i<nums.length && nums[i]==1) 7 { 8 sum++; 9 i++; 10 } 11 i++; 12 max = Math.max(sum,max); 13 } 14 return max; 15 }