问题描述:给定一个数组,数组里面元素不重复,求第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个全排列的更多相关文章

  1. *HDU2852 树状数组(求第K小的数)

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  3. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  5. poj 2449(A*求第K短路)

    题目链接:http://poj.org/problem?id=2449 思路:我们可以定义g[x]为源点到当前点的距离,h[x]为当前点到目标节点的最短距离,显然有h[x]<=h*[x](h*[ ...

  6. 面试题:m个长度为n的ordered array,求top k 个 数字

    package com.sinaWeibo.interview; import java.util.Comparator; import java.util.Iterator; import java ...

  7. 01背包之求第K优解——Bone Collector II

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...

  8. 普林斯顿大学算法课 Algorithm Part I Week 3 求第K大数 Selection

    问题 给定N个元素的数组,求第k大的数. 特例当k=0时,就是求最大值,当k=N-1时,就是求最小值. 应用顺序统计求top N排行榜 基本思想 使用快速排序方法中的分区思想,使得a[k]左侧没有更小 ...

  9. 求n^k的前缀和

    我都已经高二了,却还不知\(1^2+2^2+3^2+4^2+...+n^2\)的通式,真是惭愧. 现在说说如何求\(n^k\)的前缀和. 如果k比较小,我们可以直接差分序列手算.否则,我们可以用神奇的 ...

随机推荐

  1. SpringMVC如何接收json数据

    请求头:Content-Type=application/json数据如: {"mobile":"12345678912","smsContent&q ...

  2. [LintCode] 最后一个单词的长度

    class Solution { public: /** * @param s A string * @return the length of last word */ int lengthOfLa ...

  3. git pull报错:There is no tracking information for the current branch

    报错: There is no tracking information for the current branch. Please specify which branch you want to ...

  4. 安装 oracle 11g 发行版 出现错误:由于以下错误,Enterprise Manager配置失败,启动Database Control时出错

    解决方案如下进入dos(1)查看dbconsole状态:emctl status dbconsole 显示:Environment variable ORACLE_SID not defined. P ...

  5. unset($_COOKIE['wcookie_date'])

    foreach ($_COOKIE AS $wk => $wv) { unset($_COOKIE[$wk]); } die(); w 删除cookie

  6. action接收请求参数

    一.采用基本类型接收请求参数(get/post)在Action类中定义与请求参数同名的属性,struts2便能接收自动接收请求参数并赋给同名属性. action的代码: public class Pa ...

  7. QT Creator常用快捷键

    1.F10,F11 单步调试 2.Ctrl + / :注释/取消注释选定内容. 3.F4 :在 头文件(.h) 和 实现文件(.cpp) 之间进行切换. 4.Ctrl + i :自动格式化选中代码. ...

  8. numpy.linspace介绍

    numpy.linspace:在指定范围内返回均匀间隔的数组 In [12]: import numpy as np In [13]: result = np.linspace(1,10) #默认生成 ...

  9. yum安装mysql5.6

    1.检查系统是否安装其他版本的MYSQL数据 yum list installed | grep mysql yum -y remove mysql-libs.x86_64 2.安装及配置 wget ...

  10. bootstrap基本使用

    bootstrap是封装了css和js代码实现酷炫的效果,所以使用的时候,比如说是列表效果,直接调用它本身定义的函数就ok了 静态文件 把href='..static/..'里面改为url_for静态 ...