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.

题目意思:

又是全排列。

【LeetCode练习题】Next Permutation

【LeetCode练习题】Permutations

本题的意思是在一个全排列中,找第k个全排列。如第一个是123,第二个是132……

n在[1,9]之间。

解题思路:

开始的时候,看到这个全排列我就想到之前做的两个排列题了,一个是求出所有的排列,一个是求出给定排列的下一个排列。

于是我尝试先求出所有的排列,再根据k返回指定位置的排列。结果果然 time limit exceed!

我又尝试了一个for循环从第一个排列开始,不断的求出下一个排列直到第k个。结果果然 memory limit exceed !

于是我直到投机取巧是行不通的了,这一题一定有属于他自己的解法在里面。

这题的解法是这样子的:

  • 以 1 2 3 4 举例,n=4,k = 10. 试着在纸上画出分别以 1 2 3 4 为根节点的四棵树先。
  • 第一层,以1 2 3 4 为根节点的每一棵树都有6个叶节点,6 = 3!,在第一棵树里第二层中,以2 3 4 为根节点的三个子树分别有 2 个叶节点, 2 = 2!,以2为子节点的子树中,有1!个叶节点。再往下有0!个叶节点。
  • 开始的时候有一个vector num,依次存着 1-n,每向ret字符串里添加一个,就erase掉那个数。(发现vector的erase真是方便啊 !)
  • 注意循环之前要进行 k--。因为我们的vector下标是从0开始的,所以假如 k 等于6的话,我们的结果应该是在以1为根节点的第一个树里,用 k / 3!反而等于 1 落在了以2为根节点的树里头了。
  • 全程,注意count 和 k 的变化就行了,k / count 表示在当前层落在哪一棵数中, k % count 表示下一层的第几个叶节点。

代码如下:

class Solution {
public:
string getPermutation(int n, int k) {
vector<int> num;
int count = ;
for(int i = ; i <= n; i++){
num.push_back(i);
count *= i;
}
//count == n!
string ret = "";
k--;
for(int i = ; i < n; i++){
count = count / (n-i);
int selected = k / count;
ret += ('' + num[selected] );
num.erase(num.begin()+selected);
k = k % count;
}
return ret;
}
};

【LeetCode练习题】Permutation Sequence的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. [LeetCode] 60. Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  7. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  8. 【Leetcode】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  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 p ...

  10. 【leetcode】 Permutation Sequence

    问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...

随机推荐

  1. 企业OA面临的问题,以及解决问题的推荐

          现在的企业不管大小都趋于软件话,而办公用的OA软件更是成为了企业中不可获取的一环,一个好的软件能让企业发展的更加顺利,而一个不合适的软件可能让公司哀声怨道反而起了反作用!       OA ...

  2. hdu 1595 find the longest of the shortest(dijkstra)

    Problem Description Marica is very angry with Mirko because he found a new girlfriend and she seeks ...

  3. Selenium+Python浏览器调用:Firefox

    如何查看python selenium的API python -m pydoc -p  4567 说明: python -m pydoc表示打开pydoc模块,pydoc是查看python文档的首选工 ...

  4. notepad++和gcc搭建语言环境

    1,工具: Notepad++:http://pan.baidu.com/s/1sjlXl6X MinGW:http://pan.baidu.com/s/1qWyQ3lq 2,安装notepad++ ...

  5. ThreadPoolExecutor详解

    线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int maxi ...

  6. httpwatch 9.3怎么在ie 8上看不到

    首先,确认HttpWatch Basic加载项是否启动:打开IE,单击工具图标并选择管理加载项.确认HttpWatch Basic的状态是已启用,点击关闭.之后打开某个网页,在页面空白处右击选择Htt ...

  7. js 的try catch

    语法: try { //在此运行代码 } catch(err) { //在此处理错误 } 注意:try...catch 使用小写字母.大写字母会出错. <script language=&quo ...

  8. java学习——数组

    元素类型[] 数组名 = new 元素类型[元素个数或数组长度]; array 为引用数据类型|-数组数据类型 | 内存结构:程序在运行时,需要在内存中的分配空间.为了提高运行的效率,有对空间进行不同 ...

  9. linux定时执行

    /root/crontab-conf文件为root用户定时执行计划文件      命令:crontab -l 说明:列出定时执行的计划列表   命令:crontab -e 说明:编辑定时执行的计划文件 ...

  10. iOS应用之间的跳转与数据传递

    在开发的时候遇到需要从其他APP调用自己的APP的需求,比如从Safari中打开APP,并且传递一些信息的需要 1.首先设置自己的URL types 打开项目中的工程文件,打开info选项,在下面的U ...