Day2|977. Squares of a Sorted Array, 209. Minimum Size Subarray Sum,59. Spiral M

157 阅读3分钟

今日题目

977.有序数组的平方

209.长度最小的子数组

59.螺旋矩阵II

977. Squares of a Sorted Array

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Example 1:

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].

Example 2:

Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]

这道题的解题关键为双指针思想,并且需要另开一个数组存放结果集。

public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length]; // 用来存放结果集
        int left = 0; // 左边的指针
        int right = nums.length -1; // 右边的指针
        int k = res.length - 1; // 结果集的指针,因为是从小到大所以指针指向最末端(先存放较大的数组)

        while(left <= right){
            if(nums[left] * nums[left] > nums[right] * nums[right]){
                    res[k--] = nums[left] * nums[left++];
                    
            }else{
                    res[k--] = nums[right] * nums[right--];
              
            }
        }   

        return res;
        
    }
  • 时间复杂度O(n)

209. Minimum Size Subarray Sum

Given an array of positive integers nums and a positive integer target, return the minimal length of a

subarray

whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

 

Example 1:

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Example 2:

Input: target = 4, nums = [1,4,4]
Output: 1

Example 3:

Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
  • 遇见的问题

    只用一个for循环,导致运行超时。没有思考清楚左指针变更的条件。

image.png

public int minSubArrayLen(int target, int[] nums) {
         int sum = 0; // 存放滑动窗口内的数值总和
         int post = 0; // 定义左指针
         int min = Integer.MAX_VALUE; 

         for(int pre = 0; pre < nums.length; pre++){
             sum += nums[pre];
             // 如果发现和超过了target则将左指针向右移知道和小于target
             while(sum >= target){
                 min = Math.min(min, pre-post+1); // 选出最小的子序列
                 sum -= nums[post++]; // 不断变更左指针的位置,调整滑动窗口大小
             }
         }  

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

59. Spiral Matrix II

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

Example 1:

Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]

Example 2:

Input: n = 1
Output: [[1]]

思路如下

image.png

这道题从始至终都需要坚持左闭右开原则,不要乱了

public int[][] generateMatrix(int n) {
        int num = 1; // 定义填充数字
        int loop = 0; // 定义循环圈数
        int i,j; // 定义行数, 列数
        int start = 0; // 定义开始循环的位置
        int[][] res = new int[n][n]; // 存放螺旋矩阵
        
        // 左闭右开
        while(loop++ < n/2 ){            
            // 从左到右
            for(j = start; j < n - loop; j++){
                res[start][j] = num++; 
            }
            // 从上到下
            for(i = start; i < n - loop; i++){
                res[i][j] = num++;
            }
            // 从右到左
            for(;j >= loop; j--){
                res[i][j] = num++; 
            }
            // 从下到上
            for(; i >= loop; i--){
                res[i][j] = num++;
            }
            start++;
        }
        // 给正中间的位置赋值(如果n为奇数的话)
        if (n % 2 == 1) {
            res[start][start] = num;
        }

        return res;
    }
  • 时间复杂度O(n^2)
  • 空间复杂度O(1)

数组总结

  • 定义 数组是存放在连续内存空间上的相同类型数据的集合。

image.png

数组元素不能删除只能覆盖

image.png