[LeetCode]60. Permutation Sequence求全排列第k个
/*
n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数
*/
public String getPermutation(int n, int k) {
k--;
List<Integer> list = new ArrayList<>();
StringBuilder res = new StringBuilder();
int count =1;
//以每个数字开头的集合有多少中排列
for (int i = 2; i <= n -1; i++) {
count*=i;
}
//记录哪些数字还没用
for (int i = 1; i <=n ; i++) {
list.add(i);
}
//回合数,也就是小集合的size
int round = n-1;
while (round>=0)
{
int num = list.get(k/count);
res.append(num);
list.remove(k/count);
if (round>0)
{
k = k%count;
count/=round;
}
round--;
}
return res.toString();
}
[LeetCode]60. Permutation Sequence求全排列第k个的更多相关文章
- 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,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [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 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 60. Permutation Sequence(求全排列的第k个排列)
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.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
随机推荐
- Toolbar+DrawerLayout+NavigationView的使用
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0303/2522.html(转载) Toolbar介绍 ActionBar由于其 ...
- JZOJ【NOIP2012模拟8.9】2020.10.5 T1
逐个击破 题目 Description 三大战役的平津战场上,傅作义集团在以北平.天津为中心,东起唐山西至张家口的铁路线上摆起子一字长蛇阵,并企图在溃败时从海上南逃或向西逃窜.为了就地歼敌不让其逃走, ...
- C#Excel导出注意事项
Excel 导出 1.首先在服务器中安装office ,并且要注册2.在组件服务中 设置Microsoft.excel.appliction 属性中设置自定义加network service用户并交互 ...
- Kafka探究之路-命令小结
操作kafka之前,要先启动安装好的zk ,因为kafka的数据都保存在zk中,zk相当于是kafka的数据库吧. 安装的zk kafka 一定要按照书上,网上的教程,将相应的配置文件全部改成自己的, ...
- Windows下django项目部署 通过Apache2.4+mod_wsgi
经过几天踩坑,记录在Windows10下通过Apache2.4部署Django项目的过程 运行环境: 先说下环境,怎么安装倒是其次的,版本很重要,我是根据mod_wsgi的版本要求下载的各个版本(py ...
- uwsgi+nginx的三种配置方式
第一种 vi /etc/uwsgi.ini uwsgi --reload uwsgi.pid vi /etc/nginx/conf.d/iot.conf service nginx restart 第 ...
- Error: Cannot find module '../lib/utils/unsupported.js'
报错: nodejs : Error: Cannot find module '../lib/utils/unsupported.js' 解决办法(linux): 去node目录下:/node***/ ...
- 题解-[国家集训队]Crash的数字表格 / JZPTAB
题解-[国家集训队]Crash的数字表格 / JZPTAB 前置知识: 莫比乌斯反演 </> [国家集训队]Crash的数字表格 / JZPTAB 单组测试数据,给定 \(n,m\) ,求 ...
- 深入理解Java虚拟机(五)——JDK故障处理工具
进程状况工具:jps jps(JVM Process Status Tool) 作用 用于虚拟机中正在运行的所有进程. 显示虚拟机执行的主类名称以及这些进程的本地虚拟机唯一ID. 可以通过RMI协议查 ...
- dbeaver 驱动安装
一.背景: 在Windows 10 安装dbeaver数据库连接工具,点"测试连接"的时候出现报错如下: Error resolving dependencies Maven ...