LeetCode——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 (ie, for n = 3):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

原题链接:https://oj.leetcode.com/problems/permutation-sequence/

从n个数的全排列中找出第k个排列。

n开头的排列有(n-1)!个。k/(n-1)!可确定第一个数字,在余下的(n-1)!中找k%(n-1)!个。

public class PermutationSequence {
	public String getPermutation(int n, int k) {
		List<Integer> list = new ArrayList<Integer>();
		for(int i=1;i<=n;i++)
			list.add(i);
		int mod = 1;
		for (int i = 1; i <= n; i++) {
			mod = mod * i;
		}
		k--;
		StringBuilder builder = new StringBuilder();
		for(int i=0;i<n;i++){
			mod /= (n-i);
			int index = k / mod;
			k %= mod;
			builder.append(list.get(index));
			list.remove(index);
		}
		return builder.toString();
	}
}
时间: 2024-08-10 21:38:52

LeetCode——Permutation Sequence的相关文章

LeetCode: Permutation Sequence [059]

[题目] 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" &

[leetcode]Permutation Sequence @ Python

原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: 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&

LeetCode: Permutation Sequence 解题报告

Permutation Sequence https://oj.leetcode.com/problems/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 (ie, for n = 3): "123&

[leetcode]Permutation Sequence

问题描述: The set [1,2,3,-,n] contains a total ofn! 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" &

[LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全排列(Permutation Sequence):给出一个全排列,求其所在位置. 二.例题 1. 求下一个全排列,Next permuation Implement next permutation, which rearranges numbers into the lexicographically ne

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

【一天一道LeetCode】#60. Permutation Sequence.

一天一道LeetCode系列 (一)题目 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): 1:"123" 2:"132" ? 3 : "213" 4 :&quo

Permutation Sequence -- leetcode

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" "

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 (ie, for n = 3): Given n and k, return the kth permutation sequence. Note: Given n will be between