首页 > 编程语言 >算法刷题 Day 2 | 977.有序数组的平方 & 209.长度最小的子数组 & 59.螺旋矩阵II

算法刷题 Day 2 | 977.有序数组的平方 & 209.长度最小的子数组 & 59.螺旋矩阵II

时间:2022-12-29 22:12:40浏览次数:66  
标签:977 59 数组 nums int E6% E7% result https

977.有序数组的平方

题目建议: 本题关键在于理解双指针思想

题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/

Tips:这道题没有空间复杂度要求,最开始以为是要在原数组上修改,后来发现是自己想复杂了。

我的题解:

class Solution {
    public int[] sortedSquares(int[] nums) {
        int pLeft = 0;
        int pRight = nums.length - 1;
        int[] result = new int[nums.length];
        int pos = nums.length - 1;
        while(pLeft <= pRight){
            int squareLeft = nums[pLeft] * nums[pLeft];
            int squareRight = nums[pRight] * nums[pRight];
            if(squareLeft >= squareRight){
                result[pos--] = squareLeft;
                pLeft++;
            }
            else{
                result[pos--] = squareRight;
                pRight--;
            }
        }
        return result;
    }
}

文章讲解:https://programmercarl.com/0977.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.html

视频讲解: https://www.bilibili.com/video/BV1QB4y1D7ep

209.长度最小的子数组

题目建议: 本题关键在于理解滑动窗口,这个滑动窗口看文字讲解 还挺难理解的,建议大家先看视频讲解。 拓展题目可以先不做。

题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/

Tips:这一题有很多内容需要注意

1.滑动窗口的循环结束条件以右端点到头为准,然后在最后一次循环内部再尽可能地往右移动左端点,直到不满足题设条件。

2.Java中int的最大值可以通过Integer.MAX_VALUE获得。

3.取最小值的函数是Math.min(a,b)

我的题解:

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int pLeft = 0;
        int pRight = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;
        for(;pRight < nums.length;pRight++){
            sum += nums[pRight];
            while(sum>=target){
                int length = pRight - pLeft + 1;
                result = Math.min(length,result);
                sum -= nums[pLeft++];
            }
        }
        if(result == Integer.MAX_VALUE){
            return 0;
        }
        else{
            return result;
        }
    }
}

文章讲解:https://programmercarl.com/0209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.html

视频讲解:https://www.bilibili.com/video/BV1tZ4y1q7XE

59.螺旋矩阵II

题目建议: 本题关键还是在转圈的逻辑,在二分搜索中提到的区间定义,在这里又用上了。

题目链接:https://leetcode.cn/problems/spiral-matrix-ii/

Tips:就像贪吃蛇一样,一共四种移动方式,每次只要撞到墙或者撞到已经去过的地方,就调转到下一个方向即可,直到走完所有的格子。

我的题解:

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int[][] history = new int[n][n];
        int row = 0;
        int column = 0;
        int counter = 0;
        int movingPattern = 0; //分为0、1、2、3一共四种方向
        while(counter<n*n){
            matrix[row][column] = ++counter;
            history[row][column] = 1;
            if(movingPattern == 0){
                if(column+1 < n && history[row][column+1]==0){
                    column++;
                }
                else{
                    row++;
                    movingPattern = 1;
                }
            }
            else if(movingPattern == 1){
                if(row+1 < n && history[row+1][column]==0){
                    row++;
                }
                else{
                    column--;
                    movingPattern = 2;
                }
            }
            else if(movingPattern == 2){
                if(column-1 >=0 && history[row][column-1]==0){
                    column--;
                }
                else{
                    row--;
                    movingPattern = 3;
                }
            }
            else if(movingPattern == 3){
                if(row-1 >=0 && history[row-1][column]==0){
                    row--;
                }
                else{
                    column++;
                    movingPattern = 0;
                }
            }
        }
        return matrix;
    }
}

文章讲解:https://programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html

视频讲解:https://www.bilibili.com/video/BV1SL4y1N7mV/

数组总结

文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E6%80%BB%E7%BB%93%E7%AF%87.html

标签:977,59,数组,nums,int,E6%,E7%,result,https
From: https://www.cnblogs.com/GavinGYM/p/17013047.html

相关文章