Design Pattern 6: Cyclic Sort | Problem: Find all Missing Numbers
This commit is contained in:
parent
7551e97cd4
commit
967208e585
33
6_find_all_missing_numbers.cpp
Normal file
33
6_find_all_missing_numbers.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
/* Problem:
|
||||
* https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const std::vector<int> &&nums) {
|
||||
out << "{";
|
||||
for (const int num : nums)
|
||||
out << num << ", ";
|
||||
out << "}";
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<int> solution(const std::vector<int> &input) {
|
||||
std::vector<bool> num_exist(input.size(), false);
|
||||
std::vector<int> missing_nums;
|
||||
for (const int num : input)
|
||||
num_exist[num - 1] = true;
|
||||
for (int i = 0, upto = input.size(); i < upto; i++) {
|
||||
if (!num_exist[i])
|
||||
missing_nums.push_back(i + 1);
|
||||
}
|
||||
return missing_nums;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << solution({4, 3, 2, 7, 8, 2, 3, 1}) << '\n'
|
||||
<< solution({1, 1}) << '\n';
|
||||
return 0;
|
||||
}
|
@ -12,3 +12,5 @@ add_executable(1_longest_substring_with_k_distinct_characters 1_longest_substrin
|
||||
add_executable(2_num_of_islands 2_num_of_islands.cpp)
|
||||
|
||||
add_executable(3_squares_of_a_sorted_array 3_squares_of_a_sorted_array.cpp)
|
||||
|
||||
add_executable(6_find_all_missing_numbers 6_find_all_missing_numbers.cpp)
|
||||
|
Loading…
Reference in New Issue
Block a user