-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiteratorforCombination.cpp
More file actions
47 lines (42 loc) · 1.12 KB
/
iteratorforCombination.cpp
File metadata and controls
47 lines (42 loc) · 1.12 KB
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
40
41
42
43
44
45
46
47
// Source: https://leetcode.com/problems/iterator-for-combination/
// Author: Miao Zhang
// Date: 2021-04-21
class CombinationIterator {
public:
CombinationIterator(string characters, int combinationLength) :
chars_(rbegin(characters), rend(characters)),
length_(combinationLength),
mask_((1 << characters.size()) - 1) {
}
string next() {
hasNext();
string res;
for (int i = chars_.size() - 1; i >= 0; i--) {
if ((mask_ >> i) & 1) {
res.push_back(chars_[i]);
}
}
mask_--;
return res;
}
bool hasNext() {
while (mask_ >= 0 && __builtin_popcount(mask_) != length_) mask_--;
return mask_ > 0;
}
private:
// 111: cba, 110: cb, 101: ca
// 111:
// 110:
// 101:
// 100
// 011:
int mask_ = 0;
int length_;
string chars_;
};
/**
* 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();
*/