-
Notifications
You must be signed in to change notification settings - Fork 6
/
1286.cpp
57 lines (42 loc) · 1.12 KB
/
1286.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
45
46
47
48
49
50
51
52
53
54
55
56
57
class CombinationIterator {
string s;
int choose;
vector<int> pos;
bool flag;
public:
CombinationIterator(string characters, int combinationLength) {
s = characters;
choose = combinationLength;
for(int i = 0; i < choose; i++)
pos.push_back(i);
if(choose > s.length())
flag = false;
else
flag = true;
}
string next() {
string temp = "";
for(auto i:pos)
temp += s[i];
flag = next_pos();
return temp;
}
bool hasNext() {
return flag;
}
bool next_pos(){
int c = 0;
for(int i = pos.size() - 1; i >= 0; i--) {
if(pos[i] < s.length() - 1 - c) {
pos[i]++;
for(int j=i+1;j<pos.size();j++)
pos[j] = pos[j-1]+1;
break;
}
else if(i == 0)
return false;
c++;
}
return true;
}
};