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]
andnums[right] * nums[right]
iteratively:- If
nums[left] * nums[left] > nums[right] * nums[right]
, savenums[left]
to the last empty position of result,left++
- else, save
nums[right]
to the last empty position of result,right--
- If
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 andresult
to store the minimum length. - Traversing the
nums
array in a for loop using pointer right:- Add
nums[right]
tosum
. - While
sum > target
:- If
right - left + 1
(subarray length) is smaller than the result, setresult = right - left + 1
. - remove the first element in subarray, to check whether the
sum
of the remain subarray is greater than or equal totarget
.
- If
- Add
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/