数组-滑动窗口?

目录

题目链接:209. 长度最小的子数组 - 力扣(LeetCode)

题目链接:904. 水果成篮 - 力扣(LeetCode)

题目链接:76. 最小覆盖子串 - 力扣(LeetCode)


题目链接:209. 长度最小的子数组 - 力扣(LeetCode)

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        // start指向子数组开头,end指向组数组结尾的下一个
        int start = 0, end = 0;
        int len = nums.size();
        // minLen指最小长度,sum=子数组的和
        int minLen = 0, sum = 0;
        // 当子数组中有元素时
        while (start <= end)
        {
            // 如果和<target,则还需要在现在子数组的基础上再加一个元素,则+end元素
            if (sum < target)
            {
                // 如果end没到原数组结尾则end++
                if (end < len )
                {
                    sum += nums[end];
                    end++;
                }
                // 如果end已经到原数组结尾,则end不能再加
                else
                {
                    break;
                }
                
            }
            // 如果子数组的和>target,记录下现在子数组的长度,并开始新一轮子数组计算,start++
            else
            {
                int currentLen = end - start;
                if (minLen == 0)
                {
                    minLen = currentLen;
                }
                else if (minLen > currentLen)
                {
                    minLen = currentLen;
                }
                sum -= nums[start];
                start++;
            }
        }
        return minLen;
    }
};

题目链接:904. 水果成篮 - 力扣(LeetCode)

看了一下别人的答案,发现同样的思路我比人家代码量多一倍,既不熟悉API又啰嗦

class Solution {
public:
    // 按理说,用注释里的代码代替下一行是一样的,但是不行
    // 我这虽然写出来了但是估计路绕大了
    int totalFruit(vector<int>& fruits) {
        // 几个篮子装水果
        int typeNum=0;
        // 两个篮子
        int type1 = 0, type2 = 0;
        int start = 0, end = 0;
        // 存放收集水果的最大数目
        int maxLen = 0;
        // 存放当前收集水果的数目
        int currentLen = 0;
        int len = fruits.size();
        // 当水果树未完全遍历完毕
        while (end<len)
        {
            // 如果当前还没摘水果,则摘下水果放入第一个篮子,并给篮子赋值,继续往后走
            // if (type1==0&&type2==0)
            if(typeNum==0)
            {
                type1 = fruits[end];
                typeNum++;
                end++;
            }
            // 如果当前已经摘了一种水果,且下一个要摘的水果不是第一种,则摘下第二种水果放入第二个篮子,继续往后走
            // else if (type2 == 0 && fruits[end] != type1)
            else if(typeNum==1&&fruits[end] != type1)
            {
                type2 = fruits[end];
                typeNum++;
                end++;
            }
            // 如果已经摘了两种水果
            else
            {
                // 如果要摘的水果是两个篮子中的一种,则摘下来往后走
                if (fruits[end] == type1 || fruits[end] == type2)
                {
                    end++;
                }
                // 如果要摘得水果不是两个篮子的一种,则重新摘
                else
                {
                    // 因为当前end所指向的水果不是两个篮子的一种,也一定是下一种的第一个,所以end前面那个水果一定是end的上一种水果
                    start = end - 1;
                    // 重新给篮子赋值
                    type1 = fruits[end - 1];
                    type2 = fruits[end];
                    // 从end前一个水果开始遍历,直到找到最前面的同一种水果
                    while (fruits[start - 1] == fruits[end - 1])
                    {
                        start--;
                    }
                }
            }
            currentLen = end - start;
            if (currentLen > maxLen)
            {
                maxLen = currentLen;
            }
        }
        return maxLen;
    }
};

题目链接:76. 最小覆盖子串 - 力扣(LeetCode)

先放个链接,后期学了哈希表再来

怎么说呢,一个月前还用过unordered_map现在忘得一干二净

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值