Skip to content

Commit e98858b

Browse files
author
zongyanqi
committed
add solution comments to 041
1 parent fbc33dd commit e98858b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

041-First-Missing-Positive.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@
1010
*/
1111

1212
/**
13+
* 思路介绍
14+
* 1. 将第 i 个元素放到第 i 个坑
15+
* 2. 遍历所有的坑, 找到数字不对应的坑
16+
*
17+
* 例如
18+
* [ 3, 4, -1, 1 ]
19+
* --> [ -1, 4, 3, 1 ]
20+
* --> [ -1, 1, 3, 4 ]
21+
* --> [ 1, -1, 3, 4 ]
22+
* --> 2
23+
*
1324
* @param {number[]} nums
1425
* @return {number}
1526
*/
1627
var firstMissingPositive = function (nums) {
1728
var n = nums.length;
1829
var i = 0;
30+
// console.log(nums);
1931
while (i < n) {
2032
var c = nums[i];
2133
if (c > 0 && n < n + 1 && c !== i + 1 && nums[c - 1] !== nums[i]) {
2234
swap(nums, c - 1, i);
23-
// i--;
35+
// console.log('-->', nums);
2436
} else {
2537
i++;
2638
}
39+
2740
}
2841

2942
// console.log(nums);
@@ -40,6 +53,6 @@ function swap(nums, i, j) {
4053
}
4154

4255
// console.log(firstMissingPositive([1, 2, 0]));
43-
// console.log(firstMissingPositive([3, 4, -1, 1]));
56+
console.log(firstMissingPositive([3, 4, -1, 1]));
4457
// console.log(firstMissingPositive([1, 1]));
45-
console.log(firstMissingPositive([2, 2]));
58+
// console.log(firstMissingPositive([2, 2]));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy