485. Max Consecutive Ones

来源:互联网 发布:java 静态单例模式 编辑:程序博客网 时间:2024/06/05 17:25

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: 3Explanation: 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 and 1.
The length of input array is a positive integer and will not exceed 10,000

翻译

给定二进制数组,找到此数组中连续1的最大数。

示例1:
输入: [1,1,0,1,1,1]
输出: 3
说明:前两位数字或后三位数字是连续的1s。
连续1的最大数量为3。
注意:

输入数组只包含0和1。
输入数组的长度为正整数,不超过10,000

解题思路

public class Solution {    public int findMaxConsecutiveOnes(int[] nums) {        if(nums.length<1)            return 0;        int count=0;        int result=0;        for(int i=0;i<nums.length;i++){            if(nums[i]==1){                count++;            }else{                if(count>result)                    result=count;                count=0;            }        }        if(count>result)            result=count;        return result;    }}

欢迎加入中科院开源软件自习室:631696396

欢迎加入中科院开源软件自习室