leetcode #60 in cpp

原创 2016年05月30日 09:49:42

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.

solution: 

We don't want to generate the kth permutation by loops. If n is n^k then this is an algorithm with exponential complexity.

The hint is actually in the question. Suppose we have n = 2. Then each of 1,2,3 would have 2 rows. In general, each of 1,2,3,.....,n would have (n-1)! rows since there are (n-1)! permutations for n-1 numbers. 

We want to get the kth number. We know that each heading, say, 1,2,4...n, would have (n-1)! rows. We could directly know that the first digit of the number is, by calculating (k-1)/(n-1)!. The first digit must by number[(k-1)/(n-1)!], given number = "123456789".   

For example, n = 3, k = 3, numbers = "123". then (3-1)/(2)! = 1 and number[1] is 2. 

Now that we have had the first digits,we proceed to get the second digit.

 Since we know the first digit, we could narrow down the range we are to search. Say n=3, k =3, and the first digit is 2, then our search range is 13, 31. Note that now our search range involves n -1 = 2 numbers, which is 1 and 3

Originally we are to get the kth number. But now we narrow down the search range, we should also narrow down k, and set k = k %(n-1)!. 

Since we have n-1 numbers now, we would have (n-1) permutations and we want to find  (k%(n-1)!)th permutation in those permutation. Wait! isn't it the same questions are our original question but with n = n-1 and k = k%(n-1)! ? It is recurrent! 

This means we could do a recurrence call, and the ith call would figure out the ith digit. Every time we update n, and k and then we could go to the next recurrent call.

Code: 

class Solution {
public:
    string getPermutation(int n, int k) {
        string res= "";
        string num = "123456789";
        long int n_1factorial = 1;//(n-1)!
        for(int i = 1; i <= n-1; i ++){
            n_1factorial *=i;
        }
        k--;//kth number points to k-1 position
        return solve(res, n, k,n_1factorial, num);
    }
    string solve(string res, int n , int k, long int n_1factorial, string &num){
        if(n==1) return res+num[0];
        else{
            res+= num[k/(n_1factorial)];
            num.erase(num.begin() + k/n_1factorial);
            return solve(res, n-1, k%(n_1factorial), n_1factorial/(n-1), num);
        }
    }
   
};



【leetcode】【60】Permutation Sequence

一、问题描述 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labe...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016年03月02日 09:21
  • 432

LeetCode-60-Permutation Sequence(找规律)-Medium

题意理解: 将0-n按照从小到大的顺序进行全排序,求解第k个排列结果 题目分析: 穷举会超时,找规律, 1)第k个排列的第一个元素在0-n中的位置为(k-1)/(n-1)! 2)在剩...
  • eddy_liu
  • eddy_liu
  • 2016年03月04日 09:07
  • 1378

(Java)LeetCode-60. Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...
  • u012848330
  • u012848330
  • 2016年10月05日 23:52
  • 297

LeetCode60:Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the pe...
  • u012501459
  • u012501459
  • 2015年07月08日 10:45
  • 621

Leetcode #60. Permutation Sequence 排列组合序列 解题报告

1 解题思想这道题是要求第K个全排列组合的序列,什么意思呢,对于一个1…n的数字,我们一共会有n!个排列组合的序列,那么我们现在按照字典序为顺序,要求给出第K个序列。传统的做法呢,是使用字典序的方式进...
  • MebiuW
  • MebiuW
  • 2016年04月30日 23:06
  • 905

LeetCode 60

Permutation Sequence     这个题是求1~n (n[1~9]) 的数字的全排列的第K个序列。     一般思路是:使用一个计数器,递归去找全排列序列,找到一个计数器加一,一直到...
  • zr1076311296
  • zr1076311296
  • 2016年06月04日 14:16
  • 1600

LeetCode 64 Minimum Path Sum (数字三角形)

LeetCode 64 Minimum Path Sum (数字三角形)
  • Tc_To_Top
  • Tc_To_Top
  • 2016年08月29日 20:27
  • 534

LeetCode 60 Permutation Sequence (Python实现及详解)

【题目】   The set [1,2,3,…,n] contains a total of n! uniquepermutations.   By listing and labeling ...
  • yangjingjing9
  • yangjingjing9
  • 2017年08月01日 16:23
  • 223

[leetcode] 【分治法】 50. Pow(x, n)

Implement pow(x, n). 题意 实现幂函数。 题解 使用分治法求解。 分:将n分成n/2  直到n=0时,返回1; 治:对n为偶数,返回两数相乘的结果,奇数再乘多一个x...
  • u014654002
  • u014654002
  • 2016年06月21日 00:37
  • 383

LeetCode Solution(持续更新,java>c++)

Title Add Date AC Rates
  • disappearedgod
  • disappearedgod
  • 2014年04月13日 21:52
  • 8835
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #60 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)