Description
Submission
class Solution {
public:
string getPermutation(int n, int k) {
string res = "";
for(int i = 1; i <= n; ++i) {
res.push_back('0' + i);
}
if(k == 1) return res;
for(int i = 2; i <= k; ++i) {
for(int j = res.size() - 1; j >= 1; --j) {
if(res[j-1] < res[j]) {
int justLarger = j;
for(int k = j + 1; k < res.size(); ++k) {
if(res[k] > res[j - 1] && res[k] < res[justLarger]) {
justLarger = k;
}
}
swap(res[j-1], res[justLarger]);
sort(res.begin() + j, res.end());
break;
}
}
}
return res;
}
};