website/content/ChapterFour/0400~0499/0485.Max-Consecutive-Ones.md
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:
0 and 1.给定一个二进制数组, 计算其中最大连续1的个数。
注意:
package leetcode
func findMaxConsecutiveOnes(nums []int) int {
maxCount, currentCount := 0, 0
for _, v := range nums {
if v == 1 {
currentCount++
} else {
currentCount = 0
}
if currentCount > maxCount {
maxCount = currentCount
}
}
return maxCount
}