Skip to content

Commit 9305453

Browse files
committed
Merge branch 'main' of github.com:w2xi/leetcode
2 parents 8e363b4 + a63420a commit 9305453

7 files changed

+127
-13
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
|35|[搜索插入位置](https://leetcode.cn/problems/search-insert-position/)|[JavaScript](./algorithms/search-insert-position.js)|Easy|
3333
|48|[旋转图像](https://leetcode.cn/problems/rotate-image/)|[JavaScript](./algorithms/rotate-image.js)|Medium|
3434
|49|[字母异位词分组](https://leetcode.cn/problems/group-anagrams/)|[JavaScript](./algorithms/group-anagrams.js)|Medium|
35+
|54|[螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/)|[JavaScript](./algorithms/spiral-matrix.js)|Medium|
3536
|58|[最后一个单词的长度](https://leetcode.cn/problems/length-of-last-word/)|[JavaScript](./algorithms/length-of-last-word.js)|Easy|
37+
|59|[螺旋矩阵 II](https://leetcode.cn/problems/spiral-matrix-ii/)|[JavaScript](./algorithms/spiral-matrix-ii.js)|Medium|
3638
|53|[最大子数组和](https://leetcode.cn/problems/maximum-subarray/)|[JavaScript](./algorithms/maximum-subarray.js)|Easy|
3739
|66|[加一](https://leetcode-cn.com/problems/plus-one/)|[JavaScript](./algorithms/plus-one.js)|Easy|
3840
|69|[x 的平方根 ](https://leetcode.cn/problems/sqrtx/)|[JavaScript](./algorithms/sqrtx.js)|Easy|

algorithms/fruit-into-baskets.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* 904. 水果成篮
23
* @param {number[]} fruits
34
* @return {number}
45
*/
@@ -11,6 +12,7 @@ var totalFruit = function (fruits) {
1112

1213
for (let right = 0; right < fruits.length; right++) {
1314
const curr = fruits[right];
15+
// 存储每个水果种类出现的次数
1416
map.set(curr, (map.get(curr) || 0) + 1);
1517

1618
while (map.size > 2) {
@@ -22,6 +24,7 @@ var totalFruit = function (fruits) {
2224
}
2325
left++;
2426
}
27+
// 更新最大数目
2528
maxLen = Math.max(maxLen, right - left + 1);
2629
}
2730

algorithms/minimum-size-subarray-sum.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* @return {number}
55
*/
66
var minSubArrayLen = function (target, nums) {
7-
// 滑动窗口
7+
// 滑动窗口
8+
// 时间复杂度: O(n)
89

910
// 滑动窗口起始位置
1011
let left = 0;
@@ -19,11 +20,36 @@ var minSubArrayLen = function (target, nums) {
1920
while (sum >= target) {
2021
// 更新最小长度
2122
minLen = Math.min(minLen, right - left + 1);
23+
// 调整区间值
2224
sum -= nums[left];
25+
// 调整起始位置
2326
left++;
2427
}
2528
right++;
2629
}
2730

2831
return minLen === Number.MAX_VALUE ? 0 : minLen;
2932
};
33+
34+
35+
/**
36+
* 暴力破解
37+
* 时间复杂度: O(n^2)
38+
*/
39+
function crash(target, nums) {
40+
let minCount = 0;
41+
42+
for (let i = 0; i < nums.length; i++) {
43+
let sum = 0;
44+
for (let j = i; j < nums.length; j++) {
45+
sum += nums[j];
46+
if (sum >= target) {
47+
let subLen = j - i + 1;
48+
minCount = minCount ? Math.min(subLen, minCount) : subLen;
49+
break;
50+
}
51+
}
52+
}
53+
54+
return minCount;
55+
}

algorithms/move-zeroes.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@
33
* @return {void} Do not return anything, modify nums in-place instead.
44
*/
55
var moveZeroes = function (nums) {
6-
let index = 0;
6+
// 双指针 (二刷的写法)
77

8-
for (let i = 0; i < nums.length; i++) {
9-
if (nums[i] !== 0) {
10-
nums[index] = nums[i];
11-
index++;
8+
let slowIndex = 0;
9+
10+
for (let fastIndex = 1; fastIndex < nums.length; fastIndex++) {
11+
if (nums[slowIndex] === 0) {
12+
if (nums[fastIndex] !== 0) {
13+
swap(nums, slowIndex++, fastIndex);
14+
}
15+
} else {
16+
slowIndex++;
1217
}
1318
}
14-
for (let i = index; i < nums.length; i++) {
15-
nums[i] = 0;
16-
}
19+
20+
// let index = 0;
21+
22+
// for (let i = 0; i < nums.length; i++) {
23+
// if (nums[i] !== 0) {
24+
// nums[index] = nums[i];
25+
// index++;
26+
// }
27+
// }
28+
// for (let i = index; i < nums.length; i++) {
29+
// nums[i] = 0;
30+
// }
1731

1832
// 双指针
1933

algorithms/spiral-matrix-ii.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 59. 螺旋矩阵 II
3+
* 参考: https://leetcode.cn/problems/spiral-matrix-ii/solution/spiral-matrix-ii-mo-ni-fa-she-ding-bian-jie-qing-x/
4+
* @param {number} n
5+
* @return {number[][]}
6+
*/
7+
var generateMatrix = function (n) {
8+
let cur = 1;
9+
let top = 0;
10+
let bottom = n - 1;
11+
let left = 0;
12+
let right = n - 1;
13+
const tar = n * n;
14+
15+
const mat = Array(n)
16+
.fill(0)
17+
.map(() => Array(n).fill(0));
18+
19+
while (cur <= tar) {
20+
for (let i = left; i <= right; i++) mat[top][i] = cur++;
21+
top++;
22+
for (let i = top; i <= bottom; i++) mat[i][right] = cur++;
23+
right--;
24+
for (let i = right; i >= left; i--) mat[bottom][i] = cur++;
25+
bottom--;
26+
for (let i = bottom; i >= top; i--) mat[i][left] = cur++;
27+
left++;
28+
}
29+
30+
return mat;
31+
};

algorithms/spiral-matrix.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 54. 螺旋矩阵
3+
* 参考: https://leetcode.cn/problems/spiral-matrix/solution/cxiang-xi-ti-jie-by-youlookdeliciousc-3/
4+
* @param {number[][]} matrix
5+
* @return {number[]}
6+
*/
7+
var spiralOrder = function (matrix) {
8+
if (matrix.length === 0) return [];
9+
10+
// 赋值上下左右边界
11+
let left = 0,
12+
top = 0;
13+
let right = matrix[0].length - 1;
14+
let bottom = matrix.length - 1;
15+
let result = [];
16+
17+
while (true) {
18+
// 从左到右
19+
for (let i = left; i <= right; i++) result.push(matrix[top][i]);
20+
// 重新设定上边界. 若上边界大于下边界,则遍历完成,下同
21+
if (++top > bottom) break;
22+
23+
// 从上到下
24+
for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
25+
if (--right < left) break;
26+
27+
// 从右到左
28+
for (let i = right; i >= left; i--) result.push(matrix[bottom][i]);
29+
if (--bottom < top) break;
30+
31+
// 从下到上
32+
for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
33+
if (++left > right) break;
34+
}
35+
36+
return result;
37+
};

algorithms/squares-of-a-sorted-array.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
* @return {number[]}
55
*/
66
var sortedSquares = function (nums) {
7-
let i = 0,
8-
j = nums.length - 1;
97
let result = Array(nums.length).fill(0);
10-
let count = nums.length - 1;
8+
let i = 0;
9+
let j = nums.length - 1;
10+
// 标记 result 数组需要填充的索引值 ( 从后往前 )
11+
let count = j;
1112

1213
while (i <= j) {
1314
const left = Math.abs(nums[i]);
1415
const right = Math.abs(nums[j]);
15-
16+
// 比较绝对值大小
1617
if (right > left) {
1718
result[count--] = right * right;
1819
j--;

0 commit comments

Comments
 (0)