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. CentOS安装错误:no default or ui configuration

    靠,以后再也不用浏览器自带的下载工具下载镜像文件了,原来是下载的不完整,但是显示完全下载完毕了,真特么误导人.文件的checksum不对. references: https://www.centos ...

  2. Linux和windows动态库

    转载:http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html 态链接库技术实现和设计程序常用的技术,在Windows和Linux系 统中 ...

  3. 使用docker打造spark集群

    前提条件:安装好了docker,见我的另一篇博客,Docker安装 有两种方式, Spark官方repo里,docker文件夹下的脚本.官方的这个脚本封装很薄,尽可能把必要的信息展示出来. AMPLa ...

  4. The type R is already defined 错误解决办法

    今天在导入一个开源项目的时候遇到了The type R is already defined的错误,试过了删除R,clear project都还是报这个错,Google一下之后找到了解决办法在 Pro ...

  5. vhd镜像格式及vhd-util工具应用

    概述 VHD 是微软虚拟磁盘文件格式,qemu和tapdisk2都能支持VHD镜像格式,不仅提供基本的虚拟机卷功能,还可提供磁盘快照.磁盘扩容等高级功能 vhd-util是个linux下的开源软件,通 ...

  6. VBA:Google翻译(含tk算法)

    完整的tk算法: //源自http://translate.google.cn/ TKK=eval('((function(){var a\x3d618632403;var b\x3d14854840 ...

  7. Python的字符串与数字

    Python3.0通过“input”实现读取控制台的输入与用户实现交互.值得注意的是input接受的所有数据都是字符串,即使输入的是数字,依然会被当作字符串来处理.这就会出现一些问题,所以需要进行类型 ...

  8. IOS7 UITableView一行滑动删除后 被删除行的下一行的点击事件将被忽略解决办法

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSI ...

  9. IOS 网络判断

    Reachability *connectionNetWork= [Reachability reachabilityForInternetConnection] ; int status = [co ...

  10. 逐渐深入地理解Ajax

    Ajax的基本原理是:XMLHttpRequest对象(简称XHR对象),XHR为向服务器发送请求和解析服务器响应提供了流畅的接口.能够以异步方式从服务器获得更多信息.意味着用户不必刷新页面也能取得新 ...