Description
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.
Example 1:
1 | Input: n = 3, k = 3 |
Example 2:
1 | Input: n = 4, k = 9 |
Difficulty: Medium
Code:
1 | class Solution { |
题意
给定数字n和k,返回1到n的全排列中第k个结果。
Solution 1
若n=4,则有{1,2,3,4},此时列出其全排列顺序如下:
1 + {2,3,4}的全排列
2 + {1,3,4}的全排列
3 + {1,2,4}的全排列
4 + {1,2,3}的全排列
对第一位来说后面三个数字排列组合有3!即6种,所以当k=9换算成下标为8,肯定在上述第二组种,第一个数字的下标为8/6=1即数字2。在确定了第一位为2的情况下,剩余组合为{1,3,4},k更新为8%6=2。找后面数字的方式和第一位一样。
对第二位来说后面两个数字排列组合有2!即2种,第二个数字的下标为2/2=1即数字3。在确定了第二位为3的情况下,剩余组合为{1,4},k更新为2%2=0。
因为k已经为0,则后面的数字为其组合的升序即{1,4}。所以最终结果为2314。
1 | class Solution { |
时间复杂度: O(n)。
空间复杂度: O(n)。