【Leetcode】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.
class Solution {
public:
string getPermutation(int n, int k) {
string result;
bool used[];
memset(used, false, );
int base[];
base[] = ;
for (int i = ; i <= n; ++i) {
base[i] = base[i - ] * i;
}
--k;
for (int i = ; i < n; ++i) {
int p = k / base[n - - i];
for (int j = ; j <= n; ++j) {
if (!used[j] && p-- == ) {
result.push_back('' + j);
used[j] = true;
break;
}
}
k %= base[n - - i];
}
return result;
}
};
n个数组的全排列中,以1开头的个数(n-1)!个,同样以2..n开头的个数有(n-1)!个,这样根据(k-1)/[(n-1)!]可知第一个数是多少,依次类推...
【Leetcode】Permutation Sequence的更多相关文章
- 【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】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
- 【LeetCode】Permutation全排列
1. Next Permutation 实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合.若其已经是最大排列,则返回最小排列 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
随机推荐
- 11-03SQLserver基础--子查询语句
一.子查询--查询的嵌套(重点记忆) select MAX(age)from haha where bumen='销售部' --汇总-- select MAX(age)from haha where ...
- leetcode874
这道题直接按照题意来解,建立坐标系和移动方案,思路是比较简单的.只是需要注意需要使用set来判断是否遇到障碍,否则会超时. int robotSim(vector<int>& co ...
- adb device offline 解决办法
当电脑中的豌豆荚之类的应用打开的状态下 adb devices 显示连接状态 关闭手机助手之后,adb devices总显示 device offline 后来发现sdk platform-tool ...
- Internet Intranet Extranet
Internet: There's only one of it, and you're on it now. Intranet: An internal network local to a com ...
- 【总结整理】JS的继承
参考阮一峰的文章:http://javascript.ruanyifeng.com/oop/inheritance.html#toc4 function Shape() { this.x = 0; t ...
- ngx-bootstrap使用03 Alerts组件、利用Object.assign复制对象
1 Alerts 该组件用于给用户操作提供反馈信息或者提供一些警告信息 2 用法 2.1 下载ngx-bootstrap依赖 参考博文:点击前往 2.2 在模块级别导入AlertModule模块 技巧 ...
- SpringBoot06 统一响应格式
1 要求 每个请求成功后,后台返回的响应格式都是一致的,例如: 2 创建一个视图模型 该模型用于格式化响应数据 package cn.xiangxu.springboottest.model.data ...
- OpenNi安装示例
1.下载OpenNi https://structure.io/openni 解压,点击运行 选择安装目录,默认即可 安装过程中有弹框,选择 安装 点击 完成 在相应的安装目录下即可找到
- 解决SQL Server [Suspect]
昨天因断电,数据库 "DatabaseName" 被标记为 [Suspect],被质疑了? Search 后,以下方案最终解决了问题. USE [master] GO ALTER ...
- STL 堆的使用
本来是要写leetcode上的merge k sorted lists那道题目,这个题目我还是很熟悉的,毕竟是看过算法导论的人,但是写的过程中对堆的维护代码还是挺多的,所以我想到了STL中的堆.下面就 ...