60. Permutation Sequence (JAVA)
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:
"123"
"132"
"213"
"231"
"312"
"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)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 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 ...
- leetcode 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 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 ...
- 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 ...
随机推荐
- python3笔记十九:os和ospath模块
一:学习内容 os模块 ospath模块 获取指定目录下所有文件和目录 二:os模块 包含了普遍的操作系统功能,需要导入该模块:import os 当前所在位置目录结构为: 目录操作 1.获取当前目录 ...
- python3笔记二十三:正则表达式之其他函数
一:学习内容 re.split函数 re.finditer函数 re.sub函数 group()分组 re.compile函数 二:字符串切割---re.split函数 需要导入包:import re ...
- 运算 Kotlin(3)
运算Kotlin支持数字运算的标准集,运算被定义为相应的类成员(但编译器会将函数调用优化为相应的指令) . 参见运算符重载.对于位运算,没有特殊字符来表示,而只可用中缀方式调用命名函数,例如:val ...
- for 循环 以及 for 循环的嵌套
格式:for (; ; ) 打印20遍你好 ; i <= ; i++) { Console.WriteLine("你好"); } 输入一个整数,计算从1加到这个数的结果 Co ...
- python - lambda 函数使用
# if we need it only once and it's quite simple def make_incrementor(n): return lambda x: x + n f = ...
- 学习笔记 - Git
学习参考网址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 Git是目前世界上 ...
- Python解决数据样本类别分布不均衡问题
所谓不平衡指的是:不同类别的样本数量差异非常大. 数据规模上可以分为大数据分布不均衡和小数据分布不均衡.大数据分布不均衡:例如拥有1000万条记录的数据集中,其中占比50万条的少数分类样本便于属于这种 ...
- nohup及pip命令总结
最近在搭建Python的Web开发环境的时候,用到nohup和pip等一些工具,先简单总结一下,以备后续查用. 1.nohup nohup(no hang up)就是不挂断的意思,如果你正在运行一个进 ...
- 第三章 SpringCloud之Eureka-Client服务提供者
1.Eureka-Client简介 #################接下来开始程序啦########################## 1.pom.xml <?xml version=&qu ...
- 阶段3 2.Spring_10.Spring中事务控制_4 spring中事务控制的一组API
分析aop的 xml 的代码.更直观一些 事务提交和回滚就是我们重复的代码 spring业余事务管理器,我们拿过来直接用就可以 提交和回滚的后面直接调用释放.所以释放资源之类就是多余的 在绑定连接到线 ...