Skip to content

Commit 3a91808

Browse files
authored
Create find-all-numbers-disappeared-in-an-array.cpp
1 parent b117d8b commit 3a91808

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> findDisappearedNumbers(vector<int>& nums) {
7+
for (int i = 0; i < nums.size(); ++i) {
8+
if (nums[abs(nums[i]) - 1] > 0) {
9+
nums[abs(nums[i]) - 1] *= -1;
10+
}
11+
}
12+
13+
vector<int> result;
14+
for (int i = 0; i < nums.size(); ++i) {
15+
if (nums[i] > 0) {
16+
result.emplace_back(i + 1);
17+
} else {
18+
nums[i] *= -1;
19+
}
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)