LeetCode 题解(169): 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.

题解:

从高位起,第i个数数应为 [1,...9]中第k / factoria[n-i]个未用的数, 而最后一位数(个位)应为最后一个未用的数。k的递推公式为:

k = k % factoria[n - i]

if k == 0:

    k = factoria[n - i]。

C++版:

class Solution {
public:
    string getPermutation(int n, int k) {
        if(n == 1)
            return "1";
        vector<int> factoria(n-1, 0);
        factoria[0] = 1;
        for(int i = 1; i < n - 1; i++)
            factoria[i] = factoria[i-1] * (i + 1);
        vector<bool> used(n, false);
        int j = n - 2;
        string result;
        while(j >= 0) {
            int ith = ceil((double)k / factoria[j]);
            int i;
            for(i = 0; i < n; i++) {
                if(!used[i])
                    ith--;
                if(ith == 0) {
                    used[i] = true;
                    break;
                }
            }
            result += to_string(i + 1);
            k %= factoria[j];
            if(k == 0)
                k = factoria[j];
            j--;
        }
        for(int i = 0; i < n; i++)
            if(!used[i]) result += to_string(i + 1);
        return result;
    }
};

Java版:

public class Solution {
    public String getPermutation(int n, int k) {
        if(n == 1)
            return "1";
            
        StringBuffer result = new StringBuffer();
        int[] factoria = new int[n-1];
        boolean[] used = new boolean[n];
        factoria[0] = 1;
        for(int i = 1; i < n - 1; i++)
            factoria[i] = factoria[i-1] * (i + 1);
        int j = n - 2;
        while(j >= 0) {
            int ith = (int)Math.ceil((double)k / factoria[j]);
            int i;
            for(i = 0; i < n; i++) {
                if(!used[i])
                    ith--;
                if(ith == 0) {
                    used[i] = true;
                    break;
                }
            }
            result.append(Integer.toString(i + 1));
            k %= factoria[j];
            if(k == 0)
                k = factoria[j];
            j--;
        }
        for(int i = 0; i < n; i++)
            if(!used[i])
                result.append(Integer.toString(i + 1));
        return result.toString();
    }
}

Python版:

import math

class Solution:
    # @param {integer} n
    # @param {integer} k
    # @return {string}
    def getPermutation(self, n, k):
        if n == 1:
            return "1"
        
        used = [False for i in range(n)]
        factoria = [1 for i in range(n-1)]
        for i in range(1, n-1):
            factoria[i] = factoria[i - 1] * (i + 1)
        
        result = ""    
        j = n - 2
        while j >= 0:
            ith = int(math.ceil(float(k) / factoria[j]))
            index = 0
            for i in range(0, n):
                if not used[i]:
                    ith -= 1
                if ith == 0:
                    used[i] = True
                    index = i
                    break
            result += str(index + 1)
            k %= factoria[j]
            if k == 0:
                k = factoria[j]
            j -= 1
        
        for i in range(0, n):
            if not used[i]:
                result += str(i + 1)
        return result
            



你可能感兴趣的:(Algorithm,LeetCode,面试题)