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 ...
随机推荐
- springboot学习问题一:启动springboot报错端口被占用解决办法
一:问题 二:分析原因 springboot启动默认端口为8080,现在提示被占用,那我们可以修改springboot的启动端口,换一个未被占用的端口即可 三:解决方法 打开application.p ...
- CppCheck介绍与使用
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011012932/article/details/52778149 简述 Cppcheck 是一种 ...
- 一、基础篇--1.1Java基础-MVC设计思想
MVC简介: MVC(Model View Controller) 是模型(model)-视图(view)-控制器(controller)的缩写.一种软件设计典范,用一种业务逻辑.数据.界面显示分离的 ...
- FusionCharts的属性
FusionCharts 的 XML标签属性有一下四种数据类型 * Boolean - 布尔类型,只能为1或者0.例如:<graph showNames='1′ > * Number - ...
- PermissionUtils
import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; im ...
- Zxing二维码的集成使用
在github网站搜索Zxing 详见:https://github.com/yipianfengye/android-zxingLibrary 在module的build.gradle中执行comp ...
- while循环嵌套
<1>while嵌套的格式 while 条件1: 条件1满足时,做的事情1 条件1满足时,做的事情2 条件1满足时,做的事情3 ...(省略)... while 条件2: 条件2满足时,做 ...
- 【Java学习笔记】LinkedList JDK1.6
如下一段代码,在JDK1.6的LinkedList中,是下图这样存储的.有一个节点值为null的节点,叫做header,header的next是0,3的next是header,这是一个循环链表 Lin ...
- [转] javascript 正则表达式提取数字使用
简述: 测试了一下js的正则表达式函数match 用来从一个字符串里挑出非0开头的数字, 放入一个array里, 之后join(',')之后输出 知识点: 1. 用match函数, 提取一个字符串当中 ...
- C++ 简介 基本语法 注释
一.第一个 C++ 程序 #include <iostream> using namespace std; int main() { cout << "Hello, ...