-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
19 lines (18 loc) · 748 Bytes
/
Copy pathsolution.cpp
File metadata and controls
19 lines (18 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
vector<string> binstr(int n) {
// number of strings = 2^n
unsigned long long total = 1ULL << n;
vector<string> ans;
ans.reserve((size_t)total); // avoid repeated reallocations
for (unsigned long long mask = 0; mask < total; ++mask) {
string s(n, '0'); // build an n-length string filled with '0'
for (int j = 0; j < n; ++j) {
// check j-th bit of mask (0 = LSB). Place it at s[n-1-j] (rightmost is LSB)
if (((mask >> j) & 1ULL) != 0ULL) s[n - 1 - j] = '1';
}
ans.push_back(std::move(s)); // move s into vector to avoid copy
}
return ans;
}
};