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

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


题目链接:1493. 删掉一个元素以后全为 1 的最长子数组

代码如下:

cpp 复制代码
class Solution 
{
public:
   //滑动窗口
    int longestSubarray(vector<int>& nums)
    {
        int res = 0;
        int count = 0;//记录碰到0的个数
        int left = 0, right = 0;
        while (right < nums.size())
        {
            if (nums[right] == 0) { count++; }
            right++;
            while (count > 1)
            {
                if (nums[left] == 0) { count--; }
                left++;
            }
            res = max(res, right - left - 1);
        }
        return res;
    }
};
相关推荐
沐怡旸1 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4164 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥7 小时前
C++ std::set
c++
侃侃_天下7 小时前
最终的信号类
开发语言·c++·算法
博笙困了8 小时前
AcWing学习——差分
c++·算法
青草地溪水旁8 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁8 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥8 小时前
C++ std::vector
c++
zl_dfq9 小时前
C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
c++
每天回答3个问题9 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5