代码随想录二刷day02

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

文章目录

  • 前言
  • 一、力扣977. 有序数组的平方
  • 二、力扣209. 长度最小的子数组
  • 三、力扣904. 水果成篮
  • 四、力扣76. 最小覆盖子串
  • 五、力扣59. 螺旋矩阵 II
  • 六、力扣54. 螺旋矩阵
  • 七、力扣 剑指 Offer 29. 顺时针打印矩阵


前言

滑动窗口
先确定维护的这个滑动窗口的含义
然后一直维护这个窗口,只要能一直维护就移动右指针
一旦不能维护,就开始移动左指针,剔除元素,直到满足条件时停下来,然后再次移动右指针

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

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

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

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int count = 0, len = nums.length, min = Integer.MAX_VALUE, left = 0;
        for(int right = 0; right < len; right ++){
            count += nums[right];
            while(count >= target){
                min = Math.min(right-left+1, min);
                count -= nums[left++];
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }
}

三、力扣904. 水果成篮

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

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

class Solution {
    public String minWindow(String s, String t) {
        HashMap<Character,Integer> hs = new HashMap<Character,Integer>();
        HashMap<Character,Integer> ht = new HashMap<Character,Integer>();
        for(int i = 0;i < t.length();i ++){
            ht.put(t.charAt(i),ht.getOrDefault(t.charAt(i), 0) + 1);
        }
        String ans = "";
        int len = 0x3f3f3f3f, cnt = 0;  //有多少个元素符合
        for(int i = 0,j = 0;i < s.length();i ++)
        {
            hs.put(s.charAt(i), hs.getOrDefault(s.charAt(i), 0) + 1);
            if(ht.containsKey(s.charAt(i)) && hs.get(s.charAt(i)) <= ht.get(s.charAt(i))) cnt ++;
            while(j < i && (!ht.containsKey(s.charAt(j)) || hs.get(s.charAt(j)) > ht.get(s.charAt(j))))
            {
                int count = hs.get(s.charAt(j)) - 1;
                hs.put(s.charAt(j), count);
                j ++;
            }
            if(cnt == t.length() && i - j + 1 < len){
                len = i - j + 1;
                ans = s.substring(j,i + 1);
            }
        }
        return ans;
    }
}

五、力扣59. 螺旋矩阵 II

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int rowUp = 0, rowDown = n-1, colLeft = 0, colRight = n-1;
        for(int i = 1; i <= n * n;){
            for(int j = colLeft; j <= colRight; j ++){
                arr[rowUp][j] = i;i ++;
            }
            rowUp ++;
            for(int j = rowUp; j <= rowDown; j ++){
                arr[j][colRight] = i; i ++;
            }
            colRight --;
            for(int j = colRight; j >= colLeft; j --){
                arr[rowDown][j] = i; i ++;
            }
            rowDown --;
            for(int j = rowDown; j >= rowUp; j --){
                arr[j][colLeft] = i; i ++;
            }
            colLeft ++;
        }
        return arr;
    }
}

六、力扣54. 螺旋矩阵

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        int rowUp = 0, colRight = n-1, rowDown = m-1, colLeft = 0;
        List<Integer> list = new ArrayList<>();
        for(int i = 1; i <= m * n;){
            for(int j = colLeft; j <= colRight && i <= m * n; j ++){
                list.add(matrix[rowUp][j]);i ++;
            }
            rowUp ++;
            for(int j = rowUp; j <= rowDown && i <= m * n; j ++){
                list.add(matrix[j][colRight]); i ++;
            }
            colRight --;
            for(int j = colRight; j >= colLeft && i <= m * n; j --){
                list.add(matrix[rowDown][j]); i ++;
            }
            rowDown --;
            for(int j = rowDown; j >= rowUp && i <= m * n; j --){
                list.add(matrix[j][colLeft]); i ++;
            }
            colLeft ++;
        }
        return list;
    }
}

七、力扣 剑指 Offer 29. 顺时针打印矩阵

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0){
            return new int[0];
        }
        int m = matrix.length, n = matrix[0].length;
        int[] arr = new int[n * m];
        int rowUp = 0, colRight = n-1, rowDown = m - 1, colLeft = 0;
        for(int i = 0; i < n * m;){
            for(int j = colLeft; j <= colRight && i < n * m; j ++){
                arr[i ++] = matrix[rowUp][j];
            }
            rowUp ++;
            for(int j = rowUp; j <= rowDown && i < n * m; j ++){
                arr[i ++] = matrix[j][colRight];
            }
            colRight --;
            for(int j = colRight; j >= colLeft && i < n * m; j --){
                arr[i ++] = matrix[rowDown][j];
            }
            rowDown --;
            for(int j = rowDown; j >= rowUp && i < n * m; j --){
                arr[i ++] = matrix[j][colLeft]; 
            }
            colLeft ++;
        }
        return arr;
    }
}

你可能感兴趣的:(java,算法,数据结构,leetcode)