-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindAllLonelyNumbersInArray.cpp
More file actions
37 lines (35 loc) · 997 Bytes
/
Copy pathfindAllLonelyNumbersInArray.cpp
File metadata and controls
37 lines (35 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> findLonely(vector<int>& nums) {
int size = nums.size();
vector<int> lonely_numbers;
unordered_map<int, int> frequency_map;
for (int num : nums) {
frequency_map[num]++;
}
for (int num : nums) {
if (frequency_map[num] == 1 && frequency_map.count(num - 1) == 0 && frequency_map.count(num + 1) == 0) {
lonely_numbers.push_back(num);
}
}
return lonely_numbers;
}
};
int main() {
Solution solution;
vector<int> nums;
int num;
cout << "Input numbers:\n";
while (cin >> num && num != -1) {
nums.push_back(num);
}
vector<int> lonelyNumbers = solution.findLonely(nums);
for (int lonelyNum : lonelyNumbers) {
cout << lonelyNum << ' ';
}
}