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 (ie, 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.
解法1: Math
数学题,解法就不重复了,第一位取值每(n - 1)!变化一位,第二位取值每(n - 2)% 变化一位。 通过k / (n - i)! 算出来的是index, 这就意味着每次取完一个数以后要把那个数删除。
C++1c++ 里面vector的初始化如果用vector<int> (n, initial_value)的形式要注意是“(”, 不是“{“, 花括号是给了一组初始的array
|
|
Java1