Skip to content

Commit 9385e17

Browse files
committed
Find all numbers disappeared in an array.
1 parent 6f9e95b commit 9385e17

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
3+
4+
 
5+
6+
Example 1:
7+
Input: nums = [4,3,2,7,8,2,3,1]
8+
Output: [5,6]
9+
10+
Example 2:
11+
Input: nums = [1,1]
12+
Output: [2]
13+
 
14+
15+
Constraints:
16+
- n == nums.length
17+
- 1 <= n <= 105
18+
- 1 <= nums[i] <= n
19+
 
20+
21+
Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
22+
*/
23+
class Solution {
24+
func findDisappearedNumbers(_ nums: [Int]) -> [Int] {
25+
var arr = nums
26+
for n in arr {
27+
arr[abs(n) - 1] = -abs(arr[abs(n) - 1])
28+
}
29+
var res = [Int]()
30+
for i in 0..<arr.count {
31+
if arr[i] > 0 {
32+
res.append(i + 1)
33+
}
34+
}
35+
return res
36+
}
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/448.Find All Numbers Disappeared in an Array.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
85. [Add Strings](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/415.Add%20Strings.playground/Contents.swift)
9090
86. [Number of Segments in a String](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/434.Number%20of%20Segments%20in%20a%20String.playground/Contents.swift)
9191
87. [Arranging Coins](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/441.Arranging%20Coins.playground/Contents.swift)
92+
88. [Find All Numbers Disappeared in an Array](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/448.Find%20All%20Numbers%20Disappeared%20in%20an%20Array.playground/Contents.swift)
9293

9394
#### Medium
9495

0 commit comments

Comments
 (0)