060.Permutation Sequence

Test cases

1
2
3
4
9
54494
9
171669

Solution 1: tle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public String getPermutation(int n, int k) {
if (n < 1 || k < 1) {
return null;
}
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
permuteHelper(result, sb, n, k);
return result.get(k - 1).toString();
}
public void permuteHelper(List<String> result,
StringBuilder sb,
int n,
int k) {
if (result.size() < k) {
if (sb.length() == n) {
result.add(sb.toString());
} else {
for (int i = 1; i <= n; i++) {
if (sb.indexOf(i + "") < 0) {
sb.append(i + "");
permuteHelper(result, sb, n);
sb.setLength(sb.length() - 1);
}
}
}
}
}
}