diff --git a/Others/1286.Iterator-for-Combination/1286.Iterator-for-Combination.cpp b/Others/1286.Iterator-for-Combination/1286.Iterator-for-Combination.cpp new file mode 100644 index 000000000..9726993eb --- /dev/null +++ b/Others/1286.Iterator-for-Combination/1286.Iterator-for-Combination.cpp @@ -0,0 +1,42 @@ +class CombinationIterator { + string cur; + string end; + bool flag; + string characters; + int combinationLength; + +public: + CombinationIterator(string characters, int combinationLength) + { + cur = characters.substr(0,combinationLength); + end = characters.substr(characters.size()-combinationLength); + flag = 1; + this->characters = characters; + this->combinationLength = combinationLength; + } + + string next() + { + if (flag) + { + flag = 0; + return cur; + } + + int i = cur.size()-1; + while (i>=0 && cur[i]==characters[i+characters.size()-combinationLength]) + i--; + int j = 0; + while (cur[i]!=characters[j]) + j++; + for (int k=i; k