代码随想录四刷day1

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


接下来就开始介绍数组操作中另一个重要的方法:滑动窗口。 所谓滑动窗口,就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果。 在暴力解法中,是一个for循环滑动窗口的起始位置,一个for循环为滑动窗口的终止位置,用两个for循环 完成了一个不断搜索区间的过程。

一、力扣977. 有序数组的平方

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] arr = new int[nums.length];
        for(int i = 0, j = nums.length-1, k = j; i <= j; ){
            if(nums[i]*nums[i] <= nums[j]*nums[j]){
                arr[k --] = nums[j] * nums[j];
                j --;
            }else{
                arr[k --] = nums[i] * nums[i];
                i ++;
            }
        }
        return arr;
    }
} 

二、力扣209. 长度最小的子数组

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE, sum = 0;
        for(int i = 0, j = 0; i < nums.length;){
            if(sum < target){
                sum += nums[i ++];
            }
            while(j < i && sum >= target){
                res = Math.min(res,i-j);
                sum -= nums[j ++];
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}

三、力扣904. 水果成篮

class Solution {
    public int totalFruit(int[] fruits) {
        Map<Integer,Integer> map = new HashMap<>();
        int res = -1;
        int left = 0, right = 0;
        while(right < fruits.length){
            if(map.size() <= 2){
                res = Math.max(res, right - left);
                map.put(fruits[right], map.getOrDefault(fruits[right],0)+1);
                right ++;
            }
            while(left < right && map.size() > 2){
                map.put(fruits[left], map.get(fruits[left]) - 1);
                if(map.get(fruits[left]) == 0){
                    map.remove(fruits[left]);
                }
                left ++;
            }
            res = Math.max(res, right - left);
        }
        return res;
    }
}

四、力扣76. 最小覆盖子串

class Solution {
    public String minWindow(String s, String t) {
        Set<Character> set = new HashSet<>();
        Map<Character,Integer> map = new HashMap<>();
        int count = 0;
        for(char ch : t.toCharArray()){
            set.add(ch);
            map.put(ch, map.getOrDefault(ch,0)+1);
        }
        String res = "";
        int left = 0, right = 0, min = Integer.MAX_VALUE;
        while(right < s.length()){
            if(set.contains(s.charAt(right))){
                map.put(s.charAt(right),map.get(s.charAt(right))-1);
                if(map.get(s.charAt(right)) == 0){
                    count ++;
                }
            }
            right ++;
            while(count == set.size()){
                res = min > (right-left) ? s.substring(left,right) : res;
                min = min > (right-left) ? right-left : min;
                if(set.contains(s.charAt(left))){
                    if(map.get(s.charAt(left)) == 0){
                        count --;
                    }
                    map.put(s.charAt(left),map.get(s.charAt(left))+1);
                }
                left ++;
            }
        }
        return res;
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值