Jan 19 - Permutation Sequence; BackTracking; Factorial;

Code:

public class Solution {
    public static String getPermutation(int n, int k) {
        String result = "";
        if(n == 1) return result + 1;
        int factorial_n = factorial(n-1);
        boolean[] isUsed = new boolean[n];
        return addDigit(factorial_n, k-1, n-1, isUsed);
    }

    public static int factorial(int n){
        if(n == 1 || n == 0) return 1;
        return n*factorial(n-1);
    }

    public static String addDigit(int factorial, int k, int n, boolean[] isUsed){
    	int times = k/factorial;
        int remain = k%factorial;
            int i = 0;
            int count = 0;
            for(; i < isUsed.length; i++){
                if(!isUsed[i]) count++;
                if(count == times+1) break;

            }
            isUsed[i] = true;
            if(n == 0) return String.valueOf(i+1);
            if(n == 1){
            	String s = i+1+addDigit(1, remain, n-1, isUsed);
            	System.out.println("n == 1: "+ (i+1));
            	return s;
            }
            String s = i+1+addDigit(factorial/n, remain, n-1, isUsed);
            return s;
    }
}

  

时间: 2024-08-03 20:03:26

Jan 19 - Permutation Sequence; BackTracking; Factorial;的相关文章

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&

[Swift]LeetCode60. 第k个排列 | 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 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

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

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

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

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