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

  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.

这道题就是给出一个数字n和一个数字k,然后在由1-n的所有数字组成的排列组合中,输出第k个。规则如上(先改变后面的数字,就是比如四个数字,第一个是1234,第二个是1243)

然后这道题想清楚思路也就比较简单了。

总共有n个数字,那么以每个数字开头都有(n-1)!种,也就是说如果  1<=k<=(n-1)!  那么这个组合就会以1开头。

确定了第一个数字之后,那么就以同样的方法确定之后的数字,在第一个数字确定的基础上,以某个数字都有(n-2)!种 ,所以就可以推断出:

  求第k个字符串的时候,从前到后依次确定数字;count指的就是当前数字中,以某个数字开头的组合的种类个数。

public class Solution {
    public String getPermutation(int n, int k) {
        int[] flag = new int[n];
        String result ;
        char[] ans  = new char[n];
        int count = 1;
        for( int i = 2;i<n;i++)
            count*=i;
        for( int i = 0;i<n;i++){
            if ( k == 1){
                for( int j = 0;j<n;j++){
                    if( flag[j] == 0){
                        ans[i] = (char) (j+49);
                        i++;
                        flag[j] = 1;
                    }
                }
                return new String(ans);
            }
            int num = (k-1)/count;
            k = k - num*count;
            count = count/(n-1-i);
            for( int j = 0;j<n;j++){
                if( flag[j] == 0){
                    if( num == 0){
                        ans[i] = (char) (j+49);
                        flag[j] = 1;
                        break;
                    }else
                        num--;
                }
            }
        }
        return new String(ans);

    }
}

这道题是难得的一次提交就到达最快的时候。

时间: 2024-10-11 18:03:46

leetcode 60 Permutation Sequence ----- java的相关文章

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

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

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

问题: 对于给定序列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

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】 Permutation Sequence (middle)

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

[C++]LeetCode: 114 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" &q