【LeetCode每日一题】——1493.删掉一个元素以后全为 1 的最长子数组

一【题目类别】

  • 滑动窗口

二【题目难度】

  • 中等

三【题目编号】

  • 1493.删掉一个元素以后全为 1 的最长子数组

四【题目描述】

  • 给你一个二进制数组 nums ,你需要从中删掉一个元素。
  • 请你在删掉元素的结果数组中,返回最长的且只包含 1 的非空子数组的长度。
  • 如果不存在这样的子数组,请返回 0 。

五【题目示例】

  • 提示 1:

    • 输入:nums = [1,1,0,1]
    • 输出:3
    • 解释:删掉位置 2 的数后,[1,1,1] 包含 3 个 1 。
  • 示例 2:

    • 输入:nums = [0,1,1,1,0,1,1,0,1]
    • 输出:5
    • 解释:删掉位置 4 的数字后,[0,1,1,1,1,1,0,1] 的最长全 1 子数组为 [1,1,1,1,1] 。
  • 示例 3:

    • 输入:nums = [1,1,1]
    • 输出:2
    • 解释:你必须要删除一个元素。

六【题目提示】

  • 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
  • n u m s [ i ] 要么是 0 要么是 1 。 nums[i] 要么是 0 要么是 1 。 nums[i]要么是0要么是1

七【解题思路】

  • 利用滑动窗口的思想
  • 仔细理解题意,要保证滑动窗口内0的个数不超过1个,所以此滑动窗口中保存的数组就是如果删除某个数字后,剩下的子数组就是元素全为1的最长子数组
  • 使用右窗口去遍历数组,如果当滑动窗口中0的个数超过了1个,那么就移动左窗口,总之就是要保证滑动窗口内0的个数不超过1个
  • 最后返回最大的窗口长度即可
  • 还需要注意一个细节,题目要求我们必须删除一个元素,也就是说,最后的返回结果需要减1,因为减1之后的长度,才是全为1的最长子数组的长度

八【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n) n n n为传入数组的大小
  • 空间复杂度: O ( 1 ) O(1) O(1)

九【代码实现】

  1. Java语言版
class Solution {
    
    
    public int longestSubarray(int[] nums) {
    
    
        int len = nums.length;
        int left = 0;
        int right = 0;
        int zero_count = 0;
        int res = 0;
        while(right < len){
    
    
            if(nums[right] == 0){
    
    
                zero_count++;
            }
            while(zero_count > 1){
    
    
                if(nums[left] == 0){
    
    
                    zero_count--;
                }
                left++;
            }
            res = Math.max(res,right - left + 1);
            right++;
        }
        return res - 1;
    }
}
  1. C语言版
int longestSubarray(int* nums, int numsSize)
{
    
    
    int len = numsSize;
    int left = 0;
    int right = 0;
    int zero_count = 0;
    int res = 0;
    while(right < len)
    {
    
    
        if(nums[right] == 0)
        {
    
    
            zero_count++;
        }
        while(zero_count > 1)
        {
    
    
            if(nums[left] == 0)
            {
    
    
                zero_count--;
            }
            left++;
        }
        res = fmax(res,right - left + 1);
        right++;
    }
    return res - 1;
}
  1. Python语言版
class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        size = len(nums)
        left = 0;
        right = 0;
        zero_count = 0
        res = 0
        while right < size:
            if nums[right] == 0:
                zero_count += 1
            while zero_count > 1:
                if nums[left] == 0:
                    zero_count -= 1
                left += 1
            res = max(res,right - left + 1)
            right += 1
        return res - 1
  1. C++语言版
class Solution {
    
    
public:
    int longestSubarray(vector<int>& nums) {
    
    
        int len = nums.size();
        int left = 0;
        int right = 0;
        int zero_count = 0;
        int res = 0;
        while(right < len){
    
    
            if(nums[right] == 0){
    
    
                zero_count++;
            }
            while(zero_count > 1){
    
    
                if(nums[left] == 0){
    
    
                    zero_count--;
                }
                left++;
            }
            res = max(res,right - left + 1);
            right++;
        }
        return res - 1;
    }
};

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. C语言版
    在这里插入图片描述

  3. Python语言版
    在这里插入图片描述

  4. C++语言版
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/IronmanJay/article/details/130959526