代码随想录刷题day02|有序数组的平方&&滑动窗口

本文记录了编程学习过程中的三个问题,包括有序数组平方的两种正确解决方法、长度最小子数组的两种错误与正确写法,以及未完成的螺旋矩阵题目。作者强调了遇到问题时的调试和改进过程。
摘要由CSDN通过智能技术生成


day02学习内容

day02主要内容

  • 有序数组的平方–双指针
  • 长度最小的子数组–滑动窗口
  • 螺旋矩阵–模拟

一、有序数组的平方

977原题链接

1.错误写法1

class Solution {
    public int[] sortedSquares(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        int index = nums.length - 1;
        int[] result = new int[nums.length];
        while (i <= j) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                result[index] = nums[i] * nums[i];
                i++;
            } else if(nums[i] * nums[i] < nums[j] * nums[j]){
                result[index] = nums[j] * nums[j];
                j--;
            }
            index--;
        }
        return result;
    }
}

为啥不能这么写呢。主要是漏了等于号,在俩个元素乘积相等的时候,会跳出循环,导致死循环,具体看下面的debug代码,或者自己复制下来debug一遍就知道了。
无限死循环

2.正确写法1

    public int[] sortedSquares(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        int index = nums.length - 1;
        int[] result = new int[nums.length];
        while (i <= j) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                result[index] = nums[i] * nums[i];
                i++;
            } else if(nums[i] * nums[i] < nums[j] * nums[j]){
                result[index] = nums[j] * nums[j];
                j--;
            }
            index--;
        }
        return result;
    }

2.正确写法2

先自增再赋值,写法也是ok的

public int[] sortedSquares(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        int index = nums.length - 1;
        int[] result = new int[nums.length];
        while (i <= j) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                result[index] = nums[i] * nums[i];
                ++i;
            } else if(nums[i] * nums[i] < nums[j] * nums[j]){
                result[index] = nums[j] * nums[j];
                --j;
            }
            --index;
        }
        return result;
    }

二、长度最小的子数组

209原题链接

1.错误1

2.正确写法1

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int start = 0;
        int end=0;
        int n = nums.length;
        int sum =0;
        int result = Integer.MAX_VALUE;
        while(end<nums.length){
            sum +=nums[end];
            while(sum>=target){
                //如果找到和大于的,就缩小范围,继续找有没有更小的范围
                int subLength=end-start+1;//这里为啥要+1,以为数组下表是从0开始的,肯定得加1
                result=Math.min(result,subLength);
                //缩小查找范围,就是把起始位置往右移动。
                sum-=nums[start];
                start++;
            }
            //不管有没有找到和大于的,终点位置都要加1.
            end++;
        }
        return result==Integer.MAX_VALUE?0:result;
    }
}

3.正确写法2

只是把end的初始化放到了for循环里面,对于初学者来说,比第一种稍微难理解一点点。

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int start = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;
        for(int end=0;end<nums.length;end++){
            sum +=nums[end];
            while(sum>=target){
                int subLength=end-start+1;
                result=Math.min(result,subLength);
                sum-=nums[start];
                start++;
            }
        }
        return result==Integer.MAX_VALUE?0:result;
    }
}

三、螺旋矩阵

59原题链接

1.没写出来,想不明白,先抄答案。。。

今天上班比较忙。


总结

1.螺旋矩阵那题没做,周末记得补回来

2.思维导图

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值