LeetCode *** 60. Permutation Sequence
来源:互联网 发布:linux sleep longer 编辑:程序博客网 时间:2024/05/01 18:38
题目:
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.
分析:
代码:
class Solution {public:string getPermutation(int n, int k) {string res = "";bool *candi=new bool[n];for (int i = 0; i < n; ++i)candi[i] = false;int total = 1, pos = 1, tk = k-1;while (pos<n) {total *= pos;pos++;}pos--;while (tk&&pos) {int t = tk / total+1;int i=0, j=0;while (i < t&&j<n) {if (!candi[j])i++;j++;}res += ('0' + j);candi[j - 1] = true;tk = tk%total;total /= pos;pos--;}for (int i = 0; i<n; ++i) {if (!candi[i])res += ('0' + i + 1);}return res;}};
0 0
- LeetCode --- 60. Permutation Sequence
- [Leetcode] 60. Permutation Sequence
- [leetcode] 60.Permutation Sequence
- **LeetCode 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- [LeetCode]60. Permutation Sequence
- LeetCode *** 60. Permutation Sequence
- LeetCode 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- LeetCode 60. Permutation Sequence
- leetcode:60. Permutation Sequence
- [Leetcode] 60. Permutation Sequence
- [leetcode] 60. Permutation Sequence
- [LeetCode] 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- Leetcode-60. Permutation Sequence
- [LeetCode]--60. Permutation Sequence
- leetcode-60. Permutation Sequence
- 如何记录MySQL执行过的SQL语句?
- Mac下使用命令行和clover制作原版10.11.4启动U盘
- PHP的相关知识点
- 【BZOJ1194】[HNOI2006]潘多拉的盒子【BFS】【SCC】【拓扑排序】【DAG最长路】【自动机】
- EF监听生成的所有sql语句(包括插入,删除sql)
- LeetCode *** 60. Permutation Sequence
- boost 1.56.0 编译及使用
- 顺序栈的实现
- install ShadowSocks at ubuntu 14
- 什么是HTTP协议??
- BZOJ3345: Pku2914 Minimum Cut
- 查看Linux字符编码:
- 背包问题-堆栈-找出其中一组解(总体积为T,n件物品体积分别是w1,w2,...,w2n,找出若干件恰好装满背包)
- 堆排序