代码随想录算法训练营第二天977.有序数组的平方 59.螺旋矩阵II 209.长度最小的子数组

977.有序数组的平方 

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

题目链接:. - 力扣(LeetCode)

文章讲解:代码随想录

视频讲解: 双指针法经典题目 | LeetCode:977.有序数组的平方_哔哩哔哩_bilibili

思路

1. 最开始想直接先平方再比较,因此对于数组先进行平方操作,后续的方法和文章中一致。

注意left <= right 的边界条件,不然就会少一个元素。

   public int[] sortedSquares(int[] nums) {
       int left = 0;
       int right = nums.length-1;
       int [] nums1 = Arrays.copyOf(nums,nums.length);
       for(int i = 0 ; i<nums.length; i++){
           nums[i] = nums[i]*nums[i];//先进行平方操作
       }
       for(int i = nums.length-1; (i>=0) && (left<=right);i--){
           if(nums[left] > nums[right]){
               nums1[i] = nums[left];
               left++;

           }
           else{
               nums1[i] = nums[right];
               right--;
           }
       }
       return nums1;

    }

2. 按照文章中的解法写的

public int[] sortedSquares(int[] nums) {
       int left = 0;
       int right = nums.length-1;
       int [] nums1 = new int[nums.length];//新建数组不用复制

       for(int i = nums.length-1; left<=right;i--){//删去了对i的约束
           if(nums[left]*nums[left] > nums[right]*nums[right]){
               nums1[i] = nums[left]*nums[left];
               left++;

           }
           else{
               nums1[i] = nums[right]*nums[right];
               right--;
           }
       }
       return nums1;

    }
209.长度最小的子数组

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

题目链接:. - 力扣(LeetCode)

文章讲解:代码随想录

视频讲解:拿下滑动窗口! | LeetCode 209 长度最小的子数组_哔哩哔哩_bilibili

思路

1.对左侧指针使用while来保证窗口收缩,在移动前将左侧指针右移
2.将结果用int中最大的数字表示,保证比较时总能选到最小值

public int minSubArrayLen(int target, int[] nums) {
            int sum  = 0;
            int slow = 0;
            int fast = 0;
            int length = nums.length;
            int cnt = Integer.MAX_VALUE;//用于比较,选出最小值
            for(;fast<length;fast++){
                sum += nums[fast];
                while(sum >= target){
                    cnt = Math.min(fast - slow+1,cnt);
                    sum -= nums[slow];
                    slow++;

                }
            }
            return cnt == Integer.MAX_VALUE ? 0 :cnt;
        }

59.螺旋矩阵II

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

题目链接:. - 力扣(LeetCode)

文章讲解:代码随想录

视频讲解:一入循环深似海 | LeetCode:59.螺旋矩阵II_哔哩哔哩_bilibili

思路

1. 考虑长宽短边的奇偶性,导致最后的余项的形态不同(长宽)

public int[][] generateMatrix(int n) {
        int startX = 0;
        int startY = 0;
        int offset = 1;
        int i,j;
        int count = 1;
        int loop = 0;
        int[][] result = new int[n][n];
        while(loop<n/2){
            for(j=startY;j < n-offset;j++){
                result[startX][j]= count++;
            }

            for(i=startX;i < n-offset;i++){
                result[i][j] = count++;
            }
            for(;j > startY;j--){
                result[i][j] = count++;
            }
            for(;i > startX ;i--){
                result[i][j] = count++;
            }

            startY++;
            startX++;
            offset++;
           loop++;
        }
        if(n%2==1){
            result[n/2][n/2] = count;
        }

        return result;
    }

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值