leetcode Permutation Sequence

原创 2013年12月03日 20:26:05

Permutation Sequence

 Total Accepted: 1819 Total Submissions: 9060My Submissions

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.

Discuss



Three problems:

1. overly dependent on next_permutation, TLE

2. --k. If k%a! == b, so the bth character should be brought forward from the rear a elements.

3. If the length is n, n - 1 adjustments are enough.  So i = n instead of n - 1

for (i = n - 1; i >= 1; --i) {

class Solution {
 public:
  int factorial[10];
  string getPermutation(int n, int k) {
    string res = "";
    int i, j, tmp;
    vector<int> pos;
    factorial[0] = 1;
    for (i = 1; i <= n; ++i) {
      factorial[i] = factorial[i - 1] * i;
      res.push_back('0' + i);
    }
    --k;
    for (i = n - 1; i >= 1; --i) {
      pos.push_back(k / factorial[i]);
      k = k % factorial[i];
    }
    for (i = 0; i < pos.size(); ++i) {
      tmp = res[i + pos[i]];
      for (j = pos[i]; j >= 1; --j)
        res[i + j] = res[i + j - 1];
      res[i] = tmp;
    }
    
    return res;
  }
};

Give a WA code here, find the bug:

class Solution {
 public:
  string getPermutation(int n, int k) {
    vector<int> factorial(n, 1);
    string res(n, '1');
    for (int i = 1; i < n; ++i) {
      factorial[i] = factorial[i-1]*i;
      res[i] += i - 1;
    }
    --k;
    for (int i = 0; i < n; ++i) {
      int tmp = k / factorial[n - 1 - i];
      char ch = res[i + tmp];
      for (int j = tmp + i; j > i; --j)
        res[j] = res[j - 1];
      res[i] = ch;
      k = k % factorial[n - 1 - i];
    }
    return res;
  }
};

Another AC version:

class Solution {
 public:
  string getPermutation(int n, int k) {
    vector<int> factorial(n, 1);
    string res(n, '1');
    for (int i = 1; i < n; ++i) {
      factorial[i] = factorial[i-1]*i;
      res[i] += i;
    }
    --k;
    for (int i = 0; i < n; ++i) {
      int tmp = k / factorial[n - 1 - i];
      char ch = res[i + tmp];
      for (int j = tmp + i; j > i; --j)
        res[j] = res[j - 1];
      res[i] = ch;
      k = k % factorial[n - 1 - i];
    }
    return res;
  }
};



Leetcode: Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the ...
  • doc_sgl
  • doc_sgl
  • 2013年10月17日 17:56
  • 11730

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
  • 220

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
  • 1377

Permutation Sequence -- LeetCode

原题链接: http://oj.leetcode.com/problems/permutation-sequence/  这道题目算法上没有什么特别的,更像是一道找规律的数学题目。我们知道,n个数的...
  • linhuanmars
  • linhuanmars
  • 2014年03月25日 06:41
  • 11249

(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
  • 295

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

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

LeetCode 60. Permutation Sequence(排列序列)

原题网址:https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total o...
  • jmspan
  • jmspan
  • 2016年05月21日 09:05
  • 316

【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
  • 430

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
  • 620

60. Permutation Sequence(难)

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...
  • gao1440156051
  • gao1440156051
  • 2016年08月25日 21:30
  • 849
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Permutation Sequence
举报原因:
原因补充:

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