leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1286.cpp (743B)


0 class Solution {
1 vector<int> vec;
2 const string chars;
3 string res;
5 bool has_next = true;
6 void shuffle() {
7 int goal = chars.size() - 1, idx = vec.size() - 1;
8 while (idx > 0 && vec[idx] == goal)
9 goal--, idx--;
10 for (int i = idx, acc = vec[idx]; i < vec.size(); i++)
11 res[i] = chars[vec[i] = ++acc];
12 if (idx == 0 && vec[0] == goal) has_next = false;
13 }
15 public:
16 CombinationIterator(string chars, int len) : chars(chars), vec(len), res(len, ' ') {
17 for (int i = 0; i < len; i++)
18 res[i] = chars[vec[i] = i];
19 vec.back()--;
20 }
22 string next() {
23 shuffle();
24 return res;
25 }
27 bool hasNext() { return has_next; }
28 };