Skip to content

Commit 3230038

Browse files
committed
Add solution #448
1 parent 2a7ff77 commit 3230038

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
349|[Intersection of Two Arrays](./0349-intersection-of-two-arrays.js)|Easy|
3434
350|[Intersection of Two Arrays II](./0350-intersection-of-two-arrays-ii.js)|Easy|
3535
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
36+
448|[Find All Numbers Disappeared in an Array](./0448-find-all-numbers-disappeared-in-an-array.js)|Easy|
3637
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
3738
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
3839
541|[Reverse String II](./0541-reverse-string-ii.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 448. Find All Numbers Disappeared in an Array
3+
* https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an array `nums` of `n` integers where `nums[i]` is in the
7+
* range `[1, n]`, return an array of all the integers in the
8+
* range `[1, n]` that do not appear in nums.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums
13+
* @return {number[]}
14+
*/
15+
var findDisappearedNumbers = function(nums) {
16+
const result = [];
17+
18+
for (let i = 0; i < nums.length;) {
19+
if (nums[nums[i] - 1] !== nums[i]) {
20+
[nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]];
21+
} else {
22+
i++;
23+
}
24+
}
25+
26+
nums.forEach((v, i) => {
27+
if (v != i + 1) {
28+
result.push(i + 1);
29+
}
30+
});
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)