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. HDU-Big Number (斯特林公式)

    In many applications very large integers numbers are required. Some of these applications are using ...

  2. vue(5)联动+tab页

    来自:https://juejin.im/post/5a0c191f6fb9a04514639419 1.联动 新增 <input v-model="msg" /> & ...

  3. windows查看网络常用cmd命令

    一.ping 主要是测试本机TCP/IP协议配置正确性与当前网络现状.  ping命令的基本使用格式是: ping IP地址/主机名/域名 [-t] [-a] [-n count] [-l size] ...

  4. Centos6.10搭建Hadoop三节点分布式

    (一)安装JDK 1. 下载JDK,解压到相应的路径 2.  修改 /etc/profile 文件(文本末尾添加),保存 sudo vi /etc/profile # 配置 JAVA_HOME exp ...

  5. linux C判断文件是否存在

    access函数 功能描述: 检查调用进程是否可以对指定的文件执行某种操作.   用法: #include <unistd.h> #include <fcntl.h> int ...

  6. HTML6即将到来,你没有看错是HTML6

    HTML6提案1:对视频对象的更多控制 我们可能永远不会解决与压缩编解码器的争斗,但我们可以与之配合.不同的压缩算法可能需要更多的工作来实现,但是它们提供竞争.能对展现在页面上的视频帧提供更多控制的方 ...

  7. C# winform文件批量转编码 选择文件夹

    C# winform文件批量转编码 选择文件夹 打开指定目录 private void btnFile_Click(object sender, EventArgs e) { OpenFileDial ...

  8. SQLSERVER 自增列,值突然增大1000

    SQLSERVER 自增列,值突然增大1000https://blog.csdn.net/lichxi1002/article/details/40074247  

  9. EF的注解Annotation和Fluent API

    注意:Annotation特性标记可组合使用,也就是在一个类或属性上可以附加多个annotations特性 一.常用注解和对应的Fluent API 1.[Required]              ...

  10. [Java][Liferay] 模拟用户

    以admin的帐号登陆 Navigation to Users and Organizations -> All Users 找到你要查看的user,点击Actions->Imperson ...