-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
44 lines (39 loc) · 990 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
37
38
39
40
41
42
43
44
class CombinationIterator {
public:
string characters;
vector<string> cs;
int idx;
int n;
int combinationLength;
string t;
CombinationIterator(string characters, int combinationLength) {
idx = 0;
n = characters.size();
this->characters = characters;
this->combinationLength = combinationLength;
dfs(0);
}
string next() {
return cs[idx++];
}
bool hasNext() {
return idx < cs.size();
}
void dfs(int i) {
if (t.size() == combinationLength) {
cs.push_back(t);
return;
}
if (i == n) return;
t.push_back(characters[i]);
dfs(i + 1);
t.pop_back();
dfs(i + 1);
}
};
/**
* 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();
*/