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):
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
标签
类似题目
代码:
1 public class Solution { 2 public String getPermutation(int n, int k) { 3 List<Integer> numbers = new ArrayList<>(); 4 int[] factor = new int[n]; 5 int i, t; 6 String res = ""; 7 factor[0] = 1; 8 for (i = 1; i < n; ++i) factor[i] = factor[i - 1] * i; 9 for (i = 1; i <= n; ++i) numbers.add(i); 10 k--; 11 for (i = n - 1; i >= 0 ; --i) { 12 t = k / factor[i]; 13 res = res + numbers.get(t); 14 k = k % factor[i]; 15 numbers.remove(t); 16 } 17 return res; 18 } 19 }
Leetcode 60. Permutation Sequence
原文:http://www.cnblogs.com/Deribs4/p/6602289.html