【LeetCode练习题】Permutation Sequence
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):
"123""132""213""231""312""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的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- leetcode 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- leetcode 之 Permutation Sequence
Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【Leetcode】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 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 ...
- 【leetcode】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
随机推荐
- COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)
源代码下载:MyActiveX20081229.rar 声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改 ...
- WebGL 初探
官方网站:http://webglfundamentals.org/ WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenG ...
- Intersection of Two Linked Lists 解答
Question Write a program to find the node at which the intersection of two singly linked lists begin ...
- 剑指offer-面试题10:二进制中1的个数
题目:请实现一个函数,输入一个函数,输出该数二进制表示中1的个数.例如把9 表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. 这道题最典型的方法就是用移位统计,就比如统计9的二进制1 ...
- WPF拖动总结[转载]
WPF拖动总结 这篇博文总结下WPF中的拖动,文章内容主要包括: 1.拖动窗口 2.拖动控件 Using Visual Studio 2.1thumb控件 2.2Drag.Drop(不连续,没有中 ...
- vi/vim经常使用命令
工作模式 插入命令 a 在光标后附加文本 A 在本行行尾附加文本 i 在光标前插入 I 在本行行首插入文本 o 在光标以下插入新的一行 O 在光标上面插入新的一行 定位命令 h 左移一个字符/ 向左的 ...
- Android 启动Service服务和发送Broadcast广播的常用方法
一.先说Service服务. 1.利用setAction()方法来指定启动的Service服务 Intent intent = new Intent(); intent.setAction(" ...
- linux配置加载顺序
linux加载配置项时通过下面方式 首先 加载/etc/profile配置 然后 加载/ect/profile.d/下面的所有脚本 然后 加载当前用户 .bash_profile 然后 加载.bash ...
- R - 递推
Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平 ...
- D - Specialized Four-Digit Numbers
Description Find and list all four-digit numbers in decimal notation that have the property that the ...