-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1980.cpp
More file actions
36 lines (36 loc) · 936 Bytes
/
1980.cpp
File metadata and controls
36 lines (36 loc) · 936 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
class Solution {
public:
string int2binary(int num) {
string s;
while (num) {
s += to_string(num % 2);
num /= 2;
}
reverse(s.begin(), s.end());
return s;
}
int binary2int(string& binary) {
int ans = 0;
int n = binary.size();
int index = 1;
for (int i = n - 1; i >= 0; --i) {
ans += (binary[i] - '0') * index;
index *= 2;
}
return ans;
}
string findDifferentBinaryString(vector<string>& nums) {
int n = nums.size();
unordered_set<int> st;
for (auto& num : nums) {
st.insert(binary2int(num));
}
for (int i = 0; i < (n + 1); ++i) {
if (st.count(i)) continue;
string ans = int2binary(i);
string prefix(n - ans.size(), '0');
return prefix + ans;
}
return "-1";
}
};