当前位置:网站首页>Leetcode- number of maximum consecutive ones - simple

Leetcode- number of maximum consecutive ones - simple

2022-06-13 05:49:00 AnWenRen

title :485 Maximum continuous 1 The number of - Simple

subject

Given a binary array , Calculate the maximum continuity 1 The number of .

Example

 Input :[1,1,0,1,1,1]
 Output :3
 explain : The first two digits and the last three digits are consecutive  1 , So the greatest continuity  1  The number of is  3.

Tips

  • The input array contains only 0 and 1 .
  • The length of the input array is a positive integer , And no more than 10,000.

Code Java

		public int findMaxConsecutiveOnes(int[] nums) {
    
        int result = 0;
        int j = -1;
        for (int i = 0; i < nums.length; i++) {
    
            if (nums[i] == 0) {
    
                if (result < i - j - 1)
                    result = i - j - 1;
                j = i;
            }
        }
        if (result < nums.length - j)
            result = nums.length - j - 1;
        return result;
    }
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507175335.html