1. permutation-sequence 顺序排列第k个序列

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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the k th permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

第一位每个数字开头的序列都有(n-1)!个序列,因此n个数字所以共有n!个序列。以此类推,第二位每一个数开头都有(n-2)!个序列。
 
public class Solution {
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);
} // change k to be index
k--; // set factorial of n
int mod = 1;
for (int i = 1; i <= n; i++) {
mod = mod * i;
} String result = ""; // find sequence
for (int i = 0; i < n; i++) {
mod = mod / (n - i);
// find the right number(curIndex) of
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();
}
}

leetcode:回溯——permutation-sequence,的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. [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 ...

  4. 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 ...

  5. 【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 ...

  6. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  7. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  8. 【Leetcode】Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. 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 ...

  10. 【leetcode】 Permutation Sequence

    问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...

随机推荐

  1. Linux字符设备驱动--Led设备驱动

    ①驱动源码 #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #i ...

  2. 修改jupyter notebook的默认浏览器

    1.打开命令行 2.输入jupyter notebook --generate-config 3.显示出jupyter_notebook_config.py 文件所在的目录.按文件目录找到这个文件. ...

  3. my01_Mysql router 安装

    Mysql router 主要用途是读写分离,主主故障自动切换,负载均衡,连接池等.安装如下 下载地址:https://dev.mysql.com/downloads/router/ tar -zxv ...

  4. 信息领域热词分析系统--java爬取CSDN中文章标题即链接

    package zuoye1; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLExce ...

  5. java——volatile的可见性不能保证线程安全

    volatile: 1.保证变量对所有线程的可见性(但是由于java里面的运算并非原子操作,导致volatile变量的运算在并发下一样是不安全的) 用代码试过,确实是这样的,原因:有可能同时多个thr ...

  6. Python import搜索路径相关

    import搜索路径 在当前目录下搜索该模块 在环境变量 PYTHONPATH 中指定的路径列表中依次搜索 在 Python 安装路径的 lib 库中搜索 查看当前的搜索路径 import sys p ...

  7. silverlight vs2010 需要缺少的web组件才能加载

    在打开一个开源的Silverlight项目是遇到如图所示的问题,点击是后没有反应. 查了资料,需要安装微软的webpi(Microsoft Web Platform Installer) webpi的 ...

  8. Linux 文件锁flock 实现两个进程相互监听存活状态

    表头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依参数operation所指 ...

  9. Murano Weekly Meeting 2015.10.20

    Meeting time: 2015.October.20th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting sum ...

  10. (转) IP子网划分

    原文:http://blog.csdn.net/birdie_l/article/details/77994610  子网划分公式计算法 实例一 实例二 心算思路总结: B类公式算法举例: 总结:此表 ...