LeetCode | 977.Squares of a Sorted Array, 209. Minimum Size Subarray Sum, 59. Spiral Matrix II

977.Squares of a Sorted Array

Link: https://leetcode.com/problems/squares-of-a-sorted-array/

Description

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

Approach

Two pointers

  • Initialize two pointers: left = 0, right = nums.length - 1, and a result array.
  • Compare nums[left] * nums[left] and nums[right] * nums[right] iteratively:
    • If nums[left] * nums[left] > nums[right] * nums[right], save nums[left] to the last empty position of result, left++
    • else, save nums[right] to the last empty position of result, right--

Solution

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int left = 0;
        int right = nums.length - 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            int l = nums[left] * nums[left];
            int r = nums[right] * nums[right];
            if (l > r) {
                result[i] = l;
                left++;
            }
            else {
                result[i] = r;
                right--;
            }
        }
        return result;
    }
}

209. Minimum Size Subarray Sum

Link: https://leetcode.com/problems/minimum-size-subarray-sum/

Description

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.

Approach

Sliding Window

  • Initialize a left pointer, point to the start of the array and track the current status.
  • Initialize sum to track the sum of the elements in subarray and result to store the minimum length.
  • Traversing the nums array in a for loop using pointer right:
    • Add nums[right] to sum.
    • While sum > target:
      • If right - left + 1 (subarray length) is smaller than the result, set result = right - left + 1.
      • remove the first element in subarray, to check whether the sum of the remain subarray is greater than or equal to target.

Solution

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;

        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while(sum >= target) {
                int subLen = right - left + 1;
                result = Math.min(subLen, result);
                sum -= nums[left++];
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }
}

59. Spiral Matrix II

Link: https://leetcode.com/problems/spiral-matrix-ii/

Description

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

Approach

在这里插入图片描述

Solution

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int x = 0;
        int y = 0;
        int loop = n / 2;
        int offset = 1;
        int cnt = 1;
        while (loop >= 0) {
            int i = x;
            int j = y;
            for (j = y; j < n - offset; j++)
                result[i][j] = cnt++;
            for (i = x; i < n - offset; i++)
                result[i][j] = cnt++;
            for (; j > y; j--)
                result[i][j] = cnt++;
            for (; i > x; i--)
                result[i][j] = cnt++;
            
            x++;
            y++;
            offset++;
            loop--;
        }
        if (n % 2 == 1)
            result[n/2][n/2] = n * n;

        return result;
    }
}

Remark

Calculate the correct number of loop. And make sure to set the value to the middle of the matrix when n is odd.


Reference: https://programmercarl.com/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值