LeetCode刷题 Day02
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]
方法 1: 直接排序
代码:
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortedSquares = function(nums) {
return nums.sort((a, b) => Math.abs(a) - Math.abs(b)).map(num => num * num);
};
时间复杂度:O(nlog(n)) 空间复杂度: O(1)
方法 2: 双指针(左右指针)
- 因为结果的length已经确定,可以先allocate出空间 result,
- 比较左右两端绝对值大小,赋值后移动left/right
- result index 持续向左移动
- 左右闭区间,因为移动到中间依然需要做判断+操作
代码:
var sortedSquares = function(nums) {
let result = Array(nums.length);
let index = nums.length - 1;
let left = 0;
let right = nums.length - 1;
while (left <= right) {
if (Math.abs(nums[left]) < Math.abs(nums[right])) {
result[index] = nums[right] * nums[right];
right--;
} else {
result[index] = nums[left] * nums[left];
left++;
}
index--;
}
return result;
};
时间复杂度:O(n) 空间复杂度: 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 contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] of which the 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
滑动窗口:
- 大于等于target的时候fast pointer移动,更新sum,更新minLen
- 小于target的时候更新sum, slow pointer移动
代码:
/**
* @param {number} target
* @param {number[]} nums
* @return {number}
*/
var minSubArrayLen = function(target, nums) {
let minLen = Number.MAX_VALUE;
let slow = 0;
let sum = nums[slow];
for (let i = 0; i < nums.length;) {
if (sum < target) {
sum += nums[++i];
} else if (sum >= target) {
minLen = Math.min(minLen, i - slow + 1);
sum -= nums[slow++]
}
}
return minLen === Number.MAX_VALUE ? 0 : minLen;
};
时间复杂度: O(n) 空间复杂度: O(1)
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]]
双指针(多指针):
-
初始化二维矩阵 n*n
-
初始化top, right, bottom, left。从思路设计角度考虑,指针不需要移动到数组末尾。所以right和bottom初始化为 n - 1
-
初始化当前个数curr = 0
-
进行顺时针遍历,并不触及边界,for(let i = left; i < right; i++),保持curr更新
-
一圈结束,更新top, right, bottom, left
-
奇数个的情况在所有循环结束后 单独赋值最中间数组元素
代码:
/**
* @param {number} n
* @return {number[][]}
*/
var generateMatrix = function(n) {
let result = [];
let sum = n * n;
let curr = 1;
let left = 0;
let right = n - 1;
let top = 0;
let bottom = n - 1;
for (let i = 0; i < n; i++) {
result.push(Array(n).fill(0));
}
while (left < right) {
for (let i = left; i < right; i++) {
result[top][i] = curr;
curr++;
}
for (let i = top; i < bottom; i++) {
result[i][right] = curr;
curr++;
}
for (let i = right; i > left; i--) {
result[bottom][i] = curr;
curr++;
}
for (let i = bottom; i > top; i--) {
result[i][left] = curr;
curr++;
}
top++;
left++;
right--;
bottom--;
}
if (sum % 2) {
result[top][left] = curr;
}
return result;
};
时间复杂度:O(n2) 空间复杂度 O(n2)