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 ...
随机推荐
- (转)mysql基础命令
Sql代码 asc 按升序排列 desc 按降序排列 下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE ...
- TCP之LAST_ACK状态
前提: A:主动关闭: B:被动关闭: A执行主动关闭,发送FIN,B收到FIN,发送ACK,进入CLOSE_WAIT,B发送FIN,进入LAST_ACK等待最后一个ACK到来: 关闭方式: (1) ...
- leetcode-easy-dynamic-70 Climbing Stairs
mycode 65% class Solution(object): def climbStairs(self, n): """ :type n: int :rtype ...
- 评【TED】陆克文:中美注定要冲突吗?
陆克文TED演讲地址:https://www.bilibili.com/video/av2196100?from=search&seid=6953438337852168205 实话说,这篇材 ...
- Go语言基本类型
1.Go语言fmt包详解 fmt.Println() ###常用打印 fmt.Print() fmt.Printf() ###格式化 fmt.Sprintf() ###字符串拼接 a)普通占位符 占位 ...
- 在java poi导入Excel通用工具类示例详解
转: 在java poi导入Excel通用工具类示例详解 更新时间:2017年09月10日 14:21:36 作者:daochuwenziyao 我要评论 这篇文章主要给大家介绍了关于在j ...
- nmon服务器监控工具的使用安装
nmon是一个监控服务器性能的工具 目录 1.安装nmon 2.数据采集 1.安装nmon nmon是一种linux服务器性能监控工具,他还提供了很好的图表结果展示功能.本篇以centos6.5系统为 ...
- Python学习之==>Socket网络编程
一.计算机网络 多台独立的计算机通过网络通信设备连接起来的网络.实现资源共享和数据传递.在同一台电脑上可以将D盘上的一个文件传到C盘,但如果想从一台电脑传一个文件到另外一台电脑上就要通过计算机网络 二 ...
- Elasticsearch 6.2.3版本 string 类型字段 排序 报错 Fielddata is disabled on text fields by default
背景说明 最近在做一个 Elasticsearch 的分页查询,并且对查询结果按照特定字段进行排序的功能. 但是执行结果却报错,报错信息如下: { "error": { " ...
- Fuzzy logic
Fuzzy logic is used in artificial intelligence. In fuzzy logic, a proposition has a truth value that ...