First, preface
Second, the question 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 and digits or the last three digits is consecutive 1s. The maximum number of consecutive 1s is 3.
Note:
- The input array would be only contain and
0
1
.
- The length of input array is a positive integer and would not exceed 10,000
Three, the thinking of solving problems
Iterates through the array, counting 1 consecutive cases, comparing with the current largest 1 cluster, returning a larger value, and the final result being the maximum value.
1 classSolution {2 Public intFindmaxconsecutiveones (int[] nums) {3 intresult = 0;4 intCount = 0;5 for(inti=0; i<nums.length; i++) {6 if(Nums[i] = = 1) {7count++;8result =Math.max (count, result);9 }Ten ElseCount = 0; One } A returnresult; - } -}
Leetcode Brush title Record [java]--485 Max consecutive Ones