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下用PHPIZE安装PHP GD扩展

    环境:LNMP in centOS 6.4. linux下PHP的扩展可以用phpize的方法,比较简单地进行启用. 以下以PHP-GD2 库安装为例子. sudo yum install php-g ...

  2. newSingleThreadExecutor单任务线程池使用

    1.newSingleThreadExecutor单任务线程池,   一次只执行一个任务 package ThreadTest; import java.util.concurrent.Executo ...

  3. lnmp 架构

    Mysql安装 tar zxf mysql-boost-5.7.17.tar.gz yum install -y gcc gcc-c++ yum install cmake-2.8.12.2-4.el ...

  4. sqlserver 事务日志

    预写式日志(Write-Ahead Logging (WAL)) --在数据写入到数据库之前,先写入到日志. 1.”Begin Tran”记录  -> 缓冲区 2. 日志             ...

  5. Unable to verify your data submission.加入了_csrf也报400错误的解决

    <input type="hidden" name="_csrf" value="<?=Yii::$app->request-> ...

  6. hadoop用put上传文件时报错

    用命令-put上传文件 报错0 datanode(s) running 原因是进行了多次格式化 解决办法: 停止集群 删除在hdfs中配置的data目录(即在core-site.xml中配置的hado ...

  7. hadoop - hbase 单机版的安装部署

    1. 下载安装包. http://www.apache.org/dyn/closer.lua/hbase/ 选择版本,单机进入以下页面 2. tar xfz hbase-1.2.6-bin.tar.g ...

  8. myEclipse更换SVN登录账号

    Win10下 找到下面路径删除其下的所有文件 C:\Users\Administrator\AppData\Roaming\Subversion\auth\svn.simple AppData默认隐藏 ...

  9. ubuntu下搭建android开发环境核心篇安装AndroidStudio、sdk、jdk

    本文系转载http://blog.csdn.net/lsyz0021/article/details/52215996 一.安装前的准备 1.1.如果你还没有安装ubuntu 14.04 LTS系统, ...

  10. 修改linux文件权限命令

    修改linux文件权限命令:chmod Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文 ...