Leetcode - 485. 最大连续 1 的个数

Source

与其后悔与抱怨,不如从现在改变

在这里插入图片描述

485. 最大连续 1 的个数 - Easy

给定一个二进制数组 nums , 计算其中最大连续 1 的个数。

示例 1:

输入:nums = [1,1,0,1,1,1] 输出:3 解释:开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.

示例2:

输入:nums = [1,0,1,1,0,1] 输出:2

提示:

  • 1 <= nums.length <= 105
  • nums[i] 不是 0 就是 1.

此题被标注为Easy类别,但是初看此题,还是不太容易一次性做对,遂有此文。


1. 解读题目

题目关键点:

  1. 二进制数组( 即:num[i] 非 0 即 1 )
  2. 最大连续1的个数

2. 解法1

2.1 错误解法
class Solution {
    
      
    public int findMaxConsecutiveOnes(int[] nums) {
    
      
		// 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		// 记录遍历过程中连续1的数量
		int amountOfOne = 0;

		for(int i = 0;i < nums.length; i++){
    
      
			if(nums[i] == 1){
    
      
				amountOfOne += 1;
			}else{
    
      
				result = Math.max(result, amountOfOne);
				amountOfOne = 0;
			}
		}
		return result;
    }
}

刚开始解答此题的时候,容易写成上述代码,但是很遗憾 —— 为山九仞,功亏一篑!!!
如果在面试中,写成这样,甚是可惜。

我们来分析一下原因:
在这里插入图片描述
通过上述图示的两种情况,我们会发现上述解法的问题,就是如果数组最后一位为1,最大连续1的结果可能出现在数组的后半段,结果有误,原因在于我们没有进行result的更新操作。

2.2 正确解法
class Solution {
    
      
    public int findMaxConsecutiveOnes(int[] nums) {
    
      
		// 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		// 记录遍历过程中连续1的数量
		int amountOfOne = 0;

		for(int i = 0;i < nums.length; i++){
    
      
			if(nums[i] == 1){
    
      
				amountOfOne += 1;
			}else{
    
      
				result = Math.max(result, amountOfOne);
				amountOfOne = 0;
			}
		}
		// 我们在最后进行result的更新,这样结果就正确啦。但是我们发现有重复代码可以优化
		result = Math.max(result, amountOfOne);
		return result;
    }
}
2.2.1 正确解法-1(优化代码)
class Solution {
    
      
    public int findMaxConsecutiveOnes(int[] nums) {
    
      
		// 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		// 记录遍历过程中连续1的数量
		int amountOfOne = 0;

		for(int i = 0;i < nums.length; i++){
    
      
			if(nums[i] == 1){
    
      
				amountOfOne += 1;
			}else{
    
      
				amountOfOne = 0;
			}
		   // 无论nums[i] 是否为1,我们都进行更新result,似乎有点暴力
			result = Math.max(result, amountOfOne);
		}
		return result;
    }
}
2.2.2 正确解法-2(优化代码)
class Solution {
    
      
    public int findMaxConsecutiveOnes(int[] nums) {
    
      
		// 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		// 记录遍历过程中连续1的数量
		int amountOfOne = 0;

		for(int i = 0;i < nums.length; i++){
    
      
			if(nums[i] == 1){
    
      
				amountOfOne += 1;
				// 我们分析可以发现,只有在nums[i] = 1时,才有更新的意义
				result = Math.max(result, amountOfOne);
			}else{
    
      
				amountOfOne = 0;
			}
		}
		return result;
    }
}

在这里插入图片描述

3. 解法2 - (双指针or滑动窗口)

在这里插入图片描述

 public int findMaxConsecutiveOnes(int[] nums) {
    
      
        // 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		int left = 0;
		int right = 0;

		while(right < nums.length){
    
      
			if(nums[right] == 1){
    
      
				right++;
			}else{
    
      
				right++;
				left = right;
			}
			result = Math.max(result,right-left);
		}
		return result;
    }
3.1 优化-1
 public int findMaxConsecutiveOnes(int[] nums) {
    
      
        // 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		int left = 0;
		int right = 0;

		while(right < nums.length){
    
      
			if(nums[right] == 1){
    
      
				right++;
				// 只在nums[i]== 1的场景下,进行比对更新result
				result = Math.max(result,right-left);
			}else{
    
      
				right++;
				left = right;
			}
		}
		return result;
    }
3.2 写法优化-2

写法上的优化:即将right++ 收敛到判断条件中

 public int findMaxConsecutiveOnes(int[] nums) {
    
      
        // 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		int left = 0;
		int right = 0;

		while(right < nums.length){
    
      
			if(nums[right++] == 1){
    
      
				result = Math.max(result,right-left);
			}else{
    
      
				left = right;
			}
		}
		return result;
    }
3.3 性能优化-3

性能上的优化: 原因在于题目要求是最大连续1的个数,所以给的测试用例中0为少数,而1的数量比较多。

我们在nums[i]=0的场景下进行比对的数量将远远小于nums[i]=1的场景。

 public int findMaxConsecutiveOnes(int[] nums) {
    
      
        // 1. 处理边界条件
		if(nums == null || nums.length == 0) return 0;
		
		int result = 0;
		int left = 0;
		int right = 0;

		while(right < nums.length){
    
      
			if(nums[right] == 1){
    
      
			 	right++;	
			}else{
    
      
				result = Math.max(result,right-left);
				right++;
				left = right;
			}
		}
		result = Math.max(result,right-left);
		return result;
    }

先讲到这里。