leetcode 60 Permutation Sequence

The set [1,2,3,...,n]
contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note:
- Given n will be between 1 and 9 inclusive.
- Given k will be between 1 and n! inclusive.
分析,对于n=3,k=5;
对于n=3的排列:
- 1 + {2,3}的排列
- 2 + {1,3}的排列
- 3 + {1,2}的排列
确定最高位数值 = k-1 / (n-1)!
确定最高位后,下一位的确定和最高位一样,即可以依次迭代下去。
要注意一点,在确定完一位之后,后面的数值排列需要按照从小到大的顺序排列。
可以使用一个StringBuilder,开始时,StringBuilder中的数值为:1-n;
每确定一位,将这一位append到StringBuilder末尾,同时删除原位置的数值。
考虑到当n很大时,例如n=8时,对于k=3,前面的5位顺序未发生变化。同样的,我们每次将StringBuilder中的第0位的数值append到最后,并删除。
1 | class Solution { |
1 {2, 3, 4} 6
2 {1, 3, 4} 6
3 {1, 2, 4} 6
4 {1, 2, ,3} 6