• 滑动窗口和螺旋矩阵


    209. 长度最小的子数组

    题目

    给定一个含有 n 个正整数的数组和一个正整数 target

    找出该数组中满足其总和大于等于 target 的长度最小的 连续

    子数组

    [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度**。**如果不存在符合条件的子数组,返回 0

    示例 1:

    输入:target = 7, nums = [2,3,1,2,4,3]
    输出:2
    解释:子数组 [4,3] 是该条件下的长度最小的子数组。
    
    • 1
    • 2
    • 3

    示例 2:

    输入:target = 4, nums = [1,4,4]
    输出:1
    
    • 1
    • 2

    示例 3:

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

    答案

    class Solution {
        public int minSubArrayLen(int target, int[] nums) {
            int slow = 0;
            int sum = 0;
            int res =Integer.MAX_VALUE;
            for(int fast=0;fast<nums.length;fast++){
                sum += nums[fast];
                while(sum>=target){
                    res = Math.min(res,fast-slow+1);
                    sum -= nums[slow++];
                }
            }
            return res==Integer.MAX_VALUE ? 0 : res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15








    904. 水果成篮

    题目

    你正在探访一家农场,农场从左到右种植了一排果树。这些树用一个整数数组 fruits 表示,其中 fruits[i] 是第 i 棵树上的水果 种类

    你想要尽可能多地收集水果。然而,农场的主人设定了一些严格的规矩,你必须按照要求采摘水果:

    • 你只有 两个 篮子,并且每个篮子只能装 单一类型 的水果。每个篮子能够装的水果总量没有限制。
    • 你可以选择任意一棵树开始采摘,你必须从 每棵 树(包括开始采摘的树)上 恰好摘一个水果 。采摘的水果应当符合篮子中的水果类型。每采摘一次,你将会向右移动到下一棵树,并继续采摘。
    • 一旦你走到某棵树前,但水果不符合篮子的水果类型,那么就必须停止采摘。

    给你一个整数数组 fruits ,返回你可以收集的水果的 最大 数目。

    示例 1:

    输入:fruits = [1,2,1]
    输出:3
    解释:可以采摘全部 3 棵树。
    
    • 1
    • 2
    • 3

    示例 2:

    输入:fruits = [0,1,2,2]
    输出:3
    解释:可以采摘 [1,2,2] 这三棵树。
    如果从第一棵树开始采摘,则只能采摘 [0,1] 这两棵树。
    
    • 1
    • 2
    • 3
    • 4

    答案

    class Solution {
        public int totalFruit(int[] fruits) {
            Map<Integer,Integer> map = new HashMap();
            int res = Integer.MIN_VALUE;
            int slow = 0;
            for(int fast=0;fast<fruits.length;fast++){
                map.put(fruits[fast],map.getOrDefault(fruits[fast],0)+1);
                while(map.size()>2){
                    map.put(fruits[slow],map.get(fruits[slow])-1);
                    if(map.get(fruits[slow])==0){
                        map.remove(fruits[slow]);
                    }
                    slow++;
                }
                res = Math.max(res,fast-slow+1);
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19








    76. 最小覆盖子串

    题目

    给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 ""

    注意:

    • 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
    • 如果 s 中存在这样的子串,我们保证它是唯一的答案

    示例 1:

    输入:s = "ADOBECODEBANC", t = "ABC"
    输出:"BANC"
    解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。
    
    • 1
    • 2
    • 3

    示例 2:

    输入:s = "a", t = "a"
    输出:"a"
    解释:整个字符串 s 是最小覆盖子串。
    
    • 1
    • 2
    • 3

    答案

    
    
    
    • 1
    • 2








    59. 螺旋矩阵 II

    题目

    给你一个正整数 n ,生成一个包含 1n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

    示例 1:

    在这里插入图片描述

    输入:n = 3
    输出:[[1,2,3],[8,9,4],[7,6,5]]
    
    • 1
    • 2

    示例 2:

    输入:n = 1
    输出:[[1]]
    
    • 1
    • 2

    答案

    class Solution {
        public int[][] generateMatrix(int n) {
            int[][] res = new int[n][n];
            int start = 0;
            int count = 1;
            int loop = 1;
            int i,j;
            while(loop<=n/2){
                i= start;
                j = start;
                while(j<n-loop){
                    res[i][j++] = count++;
                }
                while(i<n-loop){
                    res[i++][j] = count++;
                }
                while(j>=loop){
                    res[i][j--] = count++;
                }
                while(i>=loop){
                    res[i--][j] = count++; 
                }
                start++;
                loop++;
            }
            if(n%2==1){
                res[start][start] = count;
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31








    54. 螺旋矩阵

    题目

    给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

    示例 1:

    在这里插入图片描述

    输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
    输出:[1,2,3,6,9,8,7,4,5]
    
    • 1
    • 2

    示例 2:

    在这里插入图片描述

    输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    输出:[1,2,3,4,8,12,11,10,9,5,6,7]
    
    • 1
    • 2

    答案

    class Solution {
        public List<Integer> spiralOrder(int[][] matrix) {
            List<Integer> res = new ArrayList();
            int left = 0,right = matrix[0].length-1;
            int top = 0,buttom = matrix.length-1;
            int i,j;
            while(left<=right && top<=buttom){
                j = left;
                i = top+1;
                while(j<=right){
                    res.add(matrix[top][j++]);
                }
                while(i<=buttom){
                    res.add(matrix[i++][right]);
                }
                if(left<right && top<buttom){
                    j = right -1;
                    i = buttom;
                    while(j>left){
                        res.add(matrix[buttom][j--]);
                    }
                    while(i>top){
                        res.add(matrix[i--][left]);
                    }
                }
                left++;
                right--;
                top++;
                buttom--;
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33








    LCR 146. 螺旋遍历二维数组

    题目

    给定一个二维数组 array,请返回「螺旋遍历」该数组的结果。

    螺旋遍历:从左上角开始,按照 向右向下向左向上 的顺序 依次 提取元素,然后再进入内部一层重复相同的步骤,直到提取完所有元素。

    示例 1:

    输入:array = [[1,2,3],[8,9,4],[7,6,5]]
    输出:[1,2,3,4,5,6,7,8,9]
    
    • 1
    • 2

    示例 2:

    输入:array  = [[1,2,3,4],[12,13,14,5],[11,16,15,6],[10,9,8,7]]
    输出:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
    
    • 1
    • 2

    答案

    class Solution {
        public int[] spiralArray(int[][] array) {
            if(array==null || array.length==0){
                return new int[]{};
            }
            int m = array.length,n = array[0].length; 
            int[] res = new int[m*n];
            int index = 0;
            int left = 0,right = n - 1;
            int top = 0,buttom = m - 1;
            int i,j;
            while(left<=right && top<=buttom){
                j = left;
                i = top + 1;
                while(j<=right){
                    res[index++] = array[top][j++];
                }
                while(i<=buttom){
                    res[index++] = array[i++][right]; 
                }
                if(left<right && top<buttom){
                    j = right - 1;
                    i = buttom;
                    while(j>left){
                        res[index++] = array[buttom][j--];
                    }
                    while(i>top){
                        res[index++] = array[i--][left];
                    }
                }
                left++;
                right--;
                top++;
                buttom--;
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    初始SpringBoot——SpringBoot的概念和使用
    Django 07
    监控搭建-Prometheus
    淘宝/天猫淘宝评论问答列表接口 API
    MySQL数据库——存储过程-游标(介绍-声明游标、打开游标、获取游标记录、关闭游标,案例)
    MongoDB CRUD操作:快照查询
    SpringBoot登入页面图片验证码
    OpenSIPS 防扫描处理
    Docker - WEB应用实例
    NISP-SO安全运维工程师需要掌握的核心能力
  • 原文地址:https://blog.csdn.net/shiboluobuding/article/details/136768696