原题题目
代码实现(首刷自解)
class CombinationIterator {
public:void backtracking(const string& characters,vector<string>& ret,vector<bool>& visit,int pos,int combinationLength,string& temp){
if(temp.size() == combinationLength){
ret.emplace_back(temp);return;} for(int i=pos;i<visit.size();++i){
if(visit[i]) continue;visit[i] = true;string str = temp+characters[i];backtracking(characters,ret,visit,i+1,combinationLength,str);visit[i] = false;}return;}vector<string> ret;int pos;CombinationIterator(string characters, int combinationLength) {
vector<bool> visit(characters.size(),false);string temp;backtracking(characters,ret,visit,0,combinationLength,temp);pos = 0;}string next() {
return ret[pos++];}bool hasNext() {
if(ret.size() == pos) return false;return true;}
};