# 485. Max Consecutive Ones 最大连续 1 的个数

@TOC

[LeetCode]

Difficulty: Easy

## # 题目描述

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:

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

## # 解题方法

### # Java解法

``````public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count =0;
for(int i=0; i<= nums.length; i++){
if(i != nums.length && nums[i] == 1){
count++ ;
}else{
if(count == 0){
continue;
}
count = 0;
}
}
}
}
``````

AC: 8 ms

### # Python解法

``````class Solution:
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
index = -1
N = len(nums)
res = 0
for i, n in enumerate(nums):
if n == 0:
index = i
else:
res = max(res, i - index)
return res
``````

## # 日期

2017 年 1 月 15 日 2018 年 11 月 ９ 日 —— 睡眠可以