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 for n = 3:

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

Given n and k, return the kth permutation sequence.

Note:

  • Given n will be between 1 and 9 inclusive.
  • Given k will be between 1 and n! inclusive.

Example 1:

Input: n = 3, k = 3
Output: "213"

Example 2:

Input: n = 4, k = 9
Output: "2314"

规律:在n!个排列中,除去第一位,后几位共有(n-1)!个排列,所以第一位的元素总是(n-1)!一组出现的。那么,第k行第一位的值就=nums[(k-1)/(n-1)!]。

阶乘的下一个状态依赖上一个状态,所以可以用动态规划存储阶乘的结果。

另外注意,JAVA中两个int数a,b除法的结果如果要保留Double,正确的写法是(Double) a/b,而不能是(Double) (a/b),后者由于先做的整数除法,返回的是截尾的整数。

class Solution {
public String getPermutation(int n, int k) {
int[] dp = new int[n];
dp[0] = 1;
for(int i = 1; i < n; i++){
dp[i] = i*dp[i-1]; //阶乘
} Boolean[] flag = new Boolean[n];
for(int i = 0; i < n; i++){
flag[i] = false;
} String s = "";
int cnt;
int num;
for(int i = 0; i < n ; i++){ //确定每一位的数字
cnt = (int) Math.ceil((double) k/dp[n-i-1]); //剩余数字(flag为false)里第cnt大的那个
k -= (cnt-1) * dp[n-i-1];
num = 0;
for(; cnt>0; num++){
if(flag[num]) continue;
cnt--; //flag为false计1
}
flag[num-1] = true;
s += num;
}
return s;
}
}

60. Permutation Sequence (JAVA)的更多相关文章

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

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

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. 60. Permutation Sequence

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

  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 60. Permutation Sequence(康托展开)

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

  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】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  8. LeetCode OJ 60. Permutation Sequence

    题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...

  9. 60. Permutation Sequence (String; Math)

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

随机推荐

  1. RocketMQ消息发送流程和高可用设计

    (源码阅读先看主线 再看支线 先点到为止 后面再详细分解) 高可用的设计就是:当producer发送消息到broker上,broker却宕机,那下一次发送如何避免发送到这个broker上,就是采用La ...

  2. hibernate entitymanager的理解

    hibernate EntityManager是围绕提供JPA编程接口的Hibernate Core的一个包装,支持JPA实体实例的生命周期,并允许你用标准的JavaPersistence查询语言编写 ...

  3. 查准率(precision)和召回率(recall)

    1.定义 查准率(precision):预测患有癌症且预测正确的人数 / 预测有多少人患有癌症 召回率(recall):预测患有癌症且预测正确的人数 / 实际有多少人患有癌症 2.关系 他俩的关系如下 ...

  4. 选题 Scrum立会报告+燃尽图 04

    本次作业要求参见:edu.cnblogs.com/campus/nenu/2019fall/homework/9913 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名:胜利点 二.S ...

  5. java 深入HashTable

    在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相 ...

  6. RN的win7开发环境部署和问题解决

    1安装node,配置环境变量 2.安装Android studioe,配置环境变量 3.安装python2 注意 Node 的版本必须高于 8.3,Python 的版本必须为 2.x(不支持 3.x) ...

  7. HTML基础之DOM

    DOM(Document Object Model 文档对象模型) 一个web页面的展示,是由html标签组合成的一个页面,js是一门语言,dom对象实际就是将html标签转换成了一个文档对象.可以通 ...

  8. Android的消息机制之ThreadLocal的工作原理

    ThreadLocal 可以把一个对象保存在指定的线程中,对象保存后,只能在指定线程中获取保存的数据,对于其他线程来说则无法获取到数据. 日常开发中 ThreadLocal 使用的地方比较少,但是系统 ...

  9. THUWC2019(?)历险记

    Day \(-?\) 搞文化. Day \(-4\) 突然发现自己复活了,然后就来机房了( Day \(-3\) 返 璞 归 真, 开 始 骆 氪 上午考试,被吊打了/kk Day \(-2\) 上午 ...

  10. git 新建项目的一些操作

    Command line instructions Git global setup git config --global user.name "Administrator" g ...