[leedcode 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):

  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.

public class Solution {
    //本题思路:第n位确定时,会有(n-1)!个不同的排列组合。因此需要得到n!,
    //1.本题用count表示,每次循环时,count递减
    //2.需要构造一个LinkedList链表,之所以不使用数组而使用链表,是因为删除链表的一个节点时,链表会自动补充之前的空缺位,
    //如果是数组,每次remove都需要重新排列。
    //3.有一个细节,求第k位,首先k要减1,再进行计算

    //一、康托展开:全排列到一个自然数的双射
    //X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!
    //ai为整数,并且0<=ai<i(1<=i<=n)
    //适用范围:没有重复元素的全排列
    public String getPermutation(int n, int k) {
        StringBuilder seq=new StringBuilder();
        List<Integer> list=new LinkedList<Integer>();
        int count=1;
        for(int i=0;i<n;i++){
            list.add(i+1);
            count=count*(i+1);
        }
        k--;/////
        for(int i=0;i<n;i++){
            count=count/(n-i);
            int index=k/count;
            seq.append(list.get(index));
            list.remove(index);
            k=k%count;

        }
        return seq.toString();

    }
}
时间: 2024-10-08 00:49:49

[leedcode 60] Permutation Sequence的相关文章

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): "123" "132" "213" "231" "312" "3

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

leetcode 60 Permutation Sequence ----- java

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

60. Permutation Sequence java solutions

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

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): "123" "132" "213" "231" "312" "

【一天一道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

60. Permutation Sequence(求全排列的第k个排列)

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

19.2.9 [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&

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&