PermutationSequence,求第k个全排列
问题描述:给定一个数组,数组里面元素不重复,求第k个全排列。
算法分析:这道题就是用到取商取模运算。
public String getPermutation(int n, int k) {
// initialize all numbers
ArrayList<Integer> numberList = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
numberList.add(i);
}
//第k个,按下标算是第k-1个
k--;
int mod = 1;
for (int i = 1; i <= n; i++) {//mod = n!
mod = mod * i;
}
String result = "";
for (int i = 0; i < n; i++) {
mod = mod / (n - i);//第一个元素后面有(n-1)!种组合,剩下的依次迭代
int curIndex = k / mod;//第一个元素的下标,剩下的依次迭代
// update k
k = k % mod;
// get number according to curIndex
result += numberList.get(curIndex);
// remove from list ..
numberList.remove(curIndex);
}
return result.toString();
}
PermutationSequence,求第k个全排列的更多相关文章
- *HDU2852 树状数组(求第K小的数)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- poj 2449(A*求第K短路)
题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...
- 面试题:m个长度为n的ordered array,求top k 个 数字
package com.sinaWeibo.interview; import java.util.Comparator; import java.util.Iterator; import java ...
- 01背包之求第K优解——Bone Collector II
http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...
- 普林斯顿大学算法课 Algorithm Part I Week 3 求第K大数 Selection
问题 给定N个元素的数组,求第k大的数. 特例当k=0时,就是求最大值,当k=N-1时,就是求最小值. 应用顺序统计求top N排行榜 基本思想 使用快速排序方法中的分区思想,使得a[k]左侧没有更小 ...
- 求n^k的前缀和
我都已经高二了,却还不知\(1^2+2^2+3^2+4^2+...+n^2\)的通式,真是惭愧. 现在说说如何求\(n^k\)的前缀和. 如果k比较小,我们可以直接差分序列手算.否则,我们可以用神奇的 ...
随机推荐
- 170227、java分布式系统开关功能设计(服务升降级)
首先讲一下开关的由来,例如东京在6月18日做店庆促销活动,在交易下单环节,可能需要调用A.B.C三个接口来完成,但是其实A和B是必须的,C只是附加的功能(例如在下单的时候做一下推荐),可有可无,在平时 ...
- Get请求-Test版
package com.fanqi.test; import java.io.DataInputStream; import java.io.IOException; import java.io.I ...
- AutoArchive settings explained
AutoArchive settings explained Applies To: Outlook 2010 More... Less AutoArchive helps manage the sp ...
- Outlook自动回复功能无法使用
Outlook2010 http://support.microsoft.com/viewkb/viewkb.aspx?contentid=2596516 Outlook2007 ...
- Ubuntu 14.04下安装GitLab
0.硬件要求 官方要求:http://doc.gitlab.com/ce/install/requirements.html CPU 1 core works supports up to 100 u ...
- JavaScript整理1
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.如何编写 1.J ...
- Mongo 查询
Mongo 查询 mongo js 遍历 db.getCollection('CPU').find({}).limit(100).sort({"time":-1}).forEa ...
- scc
CSS简介 CSS介绍 CSS(cascading style sheet,层叠样式表)是一种制作网页的新技术,现在已经为大多数浏览器所支持,成为网页设计必不可少的工具之一 CSS语法 CSS实例 每 ...
- Apache添加多端口
Apache\conf 目录下 添加端口监听 Vhost.conf简单写写
- yii2查询数据倒序显示
public function selectall(){ return $this->findBySql("SELECT * FROM article order by art_tim ...