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 ...
随机推荐
- gdb调试时忽略SIGPIPE 等信号
GDB调试网络程序时,会遇到SIGPIPE信息,默认GDB会把程序停下来,即使程序使用signal(SIGPIPE, SIG_IGN);来忽略信号.用handle命令设置一下缺省的signal的处理行 ...
- SSM三大框架整合配置详解
首先,导入框架所需要的全部jar包(此处省略...........) 第一步:先从mybatis框架开始 我们只需要在mybatis的核心配置文件sqlConfigXml里写上这么一段话,代表的是给p ...
- java实现二分法查找
一 前提 使用二分法查找的前提是:有序的数组,没有重复的数据元素.如果没有排序过的,需先排序. 二分法查找时使用场景为:数据量较大时 二 代码 package com.xiao.day01; publ ...
- Viola-Jones(人脸检测)
Viola-Jones 人脸检测 1.Haar特征抽取 ‘ 2. Adaboost 算法
- Activity的onSaveinstaceState()保存fragment状态
Activity的onCreat方法: @Override protected void onCreate(Bundle savedInstanceState) { FragmentManager s ...
- phpmyadmin python mysql全部正常显示中文的关键
1. 建表.列时在phpmyadmin中将编码设置为utf8_general_ci 2. python中使用sql连接时设定charset为utf8,注意不能是utf-8! 例如: def Conne ...
- 【flask】环境配置-python-dotenv的使用
[自动发现程序实例] 一般来说,在执行flask run命令运行程序前,我们需要提供程序实例所在模块的位置 . Flask会自动探测程序实例,自动探测存在下面这些规则: 从当前目录寻找app.py和w ...
- 创建的项目如果没有src/main/resources文件夹,如何解决?
这是刚创建的一个maven项目,由此可以看见,项目并没有存放配置文件的src/main/resources文件夹? 解决方案: (1)选中项目,右键单击,如图所示选择:Build Path --> ...
- JavaScript日常学习6
JavaScript的运算符.比较符.条件语句.循环语句.跳出循环(break.continue).标签 JavaScript的运算符.比较符.条件语句.循环语句.跳出循环(break.continu ...
- 64位编译器下,将指针转换成UINT32,不需要修改编译选项的编码方式
一些严格的64位编译器,将指针转换成UINT32,会报各种丢失精度的错误. 但很显然,有些时候,我们就是需要转换,且并不会真正丢失精度. 此时不需要修改编译选项的编码方式,有些用处了 示例如下: un ...