LeetCode 题解(169): Permutation Sequence

来源:互联网 发布:卸载ubuntu系统 编辑:程序博客网 时间:2024/04/29 11:58

题目:

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



0 0
原创粉丝点击