以res的后n-1位为新字符串的前缀,再找一个j,组成n位的字符串。
set保证不重复。
并且j要从大往小的找,才能全部搜索到。
class Solution {
public:
string crackSafe(int n, int k) {
string res(n,'0');
unordered_set<string> set;
set.insert(res);
int cnt = pow(k,n);
for(int i=0;i<cnt;++i){
string pre = res.substr(res.size()-n+1,n-1);
for(int j=k-1;j>=0;--j){
string cur = pre + to_string(j);
if(!set.count(cur)){
set.insert(cur);
res += to_string(j);
break;
}
}
}
return res;
}
};