-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
39 lines (35 loc) · 1 KB
/
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
37
38
39
class CombinationIterator {
public:
int size;
string cs;
int curr;
CombinationIterator(string characters, int combinationLength) {
int n = characters.size();
curr = (1 << n) - 1;
reverse(characters.begin(), characters.end());
cs = characters;
size = combinationLength;
}
string next() {
while (curr >= 0 && __builtin_popcount(curr) != size) --curr;
string ans;
for (int i = 0; i < cs.size(); ++i) {
if ((curr >> i) & 1) {
ans += cs[i];
}
}
reverse(ans.begin(), ans.end());
--curr;
return ans;
}
bool hasNext() {
while (curr >= 0 && __builtin_popcount(curr) != size) --curr;
return curr >= 0;
}
};
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
* string param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/