generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
36 lines (30 loc) · 897 Bytes
/
solution.cpp
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
// The solution can be done as follows:
// - First, sort the binary numbers.
// - Declare a binary number 0 as an iterator.
// - For each binary number, compare it with the iterator.
// - If it is equal, increment the iterator; otherwise, return the iterator.
#include <algorithm>
#include <string>
#include <vector>
class Solution {
public:
std::string findDifferentBinaryString(std::vector<std::string>& nums) {
// Sort the binary numbers.
std::sort(nums.begin(), nums.end());
// Declare a binary number 0 as an iterator.
std::string res(nums.size(), '0');
// For each binary number, compare it with the iterator.
for (const auto& num : nums) {
if (num != res) break;
// Increment the iterator.
auto c = res.rbegin();
*c += 1;
while (*c == '2') {
*c = '0';
++c;
*c += 1;
}
}
return res;
}
};