DAY2 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II

目录

有序数组的平方

题目:

思路:

代码:

209.长度最小的子数组

题目

代码

59.螺旋矩阵II

题目

思路

代码

总结:数组总结


有序数组的平方

题目链接:力扣题目链接

文章讲解:代码随想录

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

题目:

给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。

示例 1: 输入:nums = [-4,-1,0,3,10] 输出:[0,1,9,16,100] 解释:平方后,数组变为 [16,1,0,9,100],排序后,数组变为 [0,1,9,16,100]

示例 2: 输入:nums = [-7,-3,2,3,11] 输出:[4,9,9,49,121]

思路:

数组其实是有序的, 只不过负数平方之后可能成为最大数了。

那么数组平方的最大值就在数组的两端,不是最左边就是最右边,不可能是中间。

此时可以考虑双指针法了,i指向起始位置,j指向终止位置。

代码:

class Solution {
    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        int[] results = new int[nums.length];
        int i = nums.length-1;
        while (left <= right){
            int absLeft = Math.abs(nums[left]);
            int absRight = Math.abs(nums[right]);
            if(absLeft >= absRight){
                results[i--]= absLeft * absLeft;
                left++;
            }else{
                results[i--]= absRight * absRight;
                right--;
            }
            
        }
        return results;

    }
}

209.长度最小的子数组

题目链接:力扣

文章讲解:代码随想录

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

题目

给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。

示例:

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

代码

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

59.螺旋矩阵II

题目链接:力扣

文章讲解:代码随想录

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

题目

给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

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

思路

而求解本题依然是要坚持循环不变量原则。

模拟顺时针画矩阵的过程:

  • 填充上行从左到右
  • 填充右列从上到下
  • 填充下行从右到左
  • 填充左列从下到上

由外向内一圈一圈这么画下去。

代码

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] array = new int[n][n];
        int startRow = 0;
        int startCol =0;
        int endRow = n - 1;
        int endCol = n - 1;
        int count = 1;
        while(startRow <= endRow && startCol <= endCol){
            for(int col = startCol;col < endCol; col ++){
                array[startRow][col]=count++;
            }
            for(int row= startRow; row < endRow; row ++){
                array[row][endCol]=count++;
            }
            for(int col = endCol; col > startCol; col--){
                array[endRow][col] = count++;
            }
            for(int row = endRow; row> startRow; row --){
                array[row][startCol]= count ++;
            }
            startRow++;
            startCol++;
            endRow --;
            endCol --;
        }
        if(n % 2==1){
            array[n/2][n/2]=n*n;
        }

        return array;

    }
}

总结:数组总结

数组理论基础

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值