Skip to content

Commit 71bacba

Browse files
authored
Update 0041-first-missing-positive.js
I have fixed inconsistent spacing.
1 parent 52e3699 commit 71bacba

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
1-
/**
2-
* Time O(n) | Space O(1)
1+
/**
2+
* https://leetcode.com/problems/first-missing-positive/
3+
* Time O(n) | Space O(1)
34
* @param {number[]} nums
45
* @return {number}
56
*/
67
var firstMissingPositive = function(nums) {
78
//-1 1 3 4
8-
const swap = (a,b) => {
9+
const swap = (a, b) => {
910
const temp = nums[a];
1011
nums[a] = nums[b];
1112
nums[b] = temp;
1213
}
14+
1315
// swap positions
14-
for(let i = 0; i < nums.length; i++) {
16+
for (let i = 0; i < nums.length; i++) {
1517
const el = nums[i];
1618
const chair = el - 1;
17-
if(el >= 1 && el <= nums.length + 1 && nums[chair] !== el) {
18-
swap(chair, i);
19-
i--; // this decrement is important // check this input [3,4,-1,1]
19+
if (el >= 1 && el <= nums.length + 1 && nums[chair] !== el) {
20+
swap(chair, i);
21+
i--; // this decrement is important // check this input [3,4,-1,1]
2022
}
2123
}
2224

23-
24-
for(let i = 0; i < nums.length; i++) {
25-
if(nums[i] !== i+1) return i+1;
25+
for (let i = 0; i < nums.length; i++) {
26+
if (nums[i] !== i + 1) return i + 1;
2627
}
2728

2829
return nums.length + 1;
2930
};
3031

3132
/**
32-
* https://leetcode.com/problems/first-missing-positive/
3333
* Time O(n) | Space O(n)
3434
* @param {number[]} nums
3535
* @return {number}
3636
*/
3737
var firstMissingPositive1 = function(nums) {
38-
3938
const numberSet = new Set(nums);
4039

41-
let i = 1;
42-
while(numberSet.has(i)) {
43-
i++;
44-
}
45-
46-
return i;
47-
};
40+
let i = 1;
41+
while (numberSet.has(i)) {
42+
i++;
43+
}
44+
45+
return i;
46+
};

0 commit comments

Comments
 (0)