# 【Leetcode题】485. 最大连续1的个数

``````输入: [1,1,0,1,1,1]

``````

``````输入的数组只包含 0 和1。

``````

``````class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int max = 0;
int temp = 0;
for(int i=0;i<nums.length;i++){
if(nums[i] == 1){
temp ++;
}
if(nums[i] == 0){
//比较max 和 temp
if(max < temp){
max = temp;
}
temp = 0;
}
}
return max;
}
}
``````

``````class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int max = 0;
int temp = 0;
for(int i=0;i<nums.length;i++){
if(nums[i] == 1){
temp ++;
}
if(nums[i] == 0){
//比较max 和 temp
if(max < temp){
max = temp;
}
temp = 0;
}
}
if(temp > max) {
return temp;
}
return max;
}
}
``````

``````class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int max = 0;
int temp = 0;
for(int i=0;i<nums.length;i++){
if(nums[i] == 1){
temp ++;
if(temp > max){
max = temp;
}
}
else{
temp = 0;
}
}
return max;
}
}
``````

``````class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int index = -1;
int max = 0;
for(int i=0;i<nums.length;i++) {
if(nums[i] == 0) { //遇到0了，要对比下
int temp = i - 1 - index;
max = Math.max(temp, max);
index = i;
}
}
max = Math.max(max, nums.length-1-index);
return max;
}
}
``````