Java for LeetCode 060 Permutation Sequence
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 (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
解题思路:
之前曾在Java for LeetCode 046 Permutations和Java for LeetCode 031 Next Permutation做过关于字典排序的算法,但是可以肯定,这种近乎暴力枚举的算法肯定是通不过测试的!通过观察发现k / factorial(n - 1)其实就代表了str的第一个字母,顺着这个思路以此类推即可,JAVA实现如下:
static public String getPermutation(int n, int k) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < n; i++)
str.append(i + 1);
StringBuilder sb = new StringBuilder();
while (n > 1) {
int index = k / factorial(n - 1);
k %= factorial(n - 1);
if(k==0){
index--;
sb.append(str.charAt(index));
str.deleteCharAt(index);
sb.append(new StringBuilder(str).reverse());
return sb.toString();
}
sb.append(str.charAt(index));
str.deleteCharAt(index);
n--;
}
return "1";
} static int factorial(int n) {
if (n == 1)
return 1;
else
return (factorial(n - 1) * n);
}
Java for LeetCode 060 Permutation Sequence的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【LeetCode】060. 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 the p ...
- 【leetcode】 Permutation Sequence (middle)
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 之 Permutation Sequence
Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【Leetcode】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 the p ...
随机推荐
- MySQL安装最后一步apply security settings错误
网上查了很久都是说删除各种文件什么的,直接百度apply security settings,说是mysql没卸载干净.不是的. 看日志发现 You must SET PASSWORD before ...
- 与Java Web Service相关的若干概念(JAX-WS,JAX-RS)
WS ,JAX-WS ,JAX-RS,REST,Restlet,SOAP l JWS: 是指与webservice相关的J2EE(其实现在应该叫做Java EE吧)技术叫做 JWS(全称就是 jav ...
- java时间库Joda-Time
虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...
- 用word写博客
都知道word的编辑功能强大,那如何用word写博客呢? 以博客园为例 1.写好word文档后,文件->共享->发送至博客,或者新建博客模板 2. 再博客的界面点击管理账户,新建账户,如果 ...
- day4作业之信息表
实在是太low了,终究是自己写的,记录下 #!/usr/bin/env python # coding=utf8 import os, re #这里我把查询这块分为3个函数了,纠结了很久是放一起还是分 ...
- MSF溢出实战教程
1. 进入终端,开启MSF相关服务 2. 连接数据库 3. 主机扫描 发现如果有MS08_067漏洞,就可以继续渗透 4. 开始溢出 溢出成功的话 sessions -l 查看 ...
- 行为Behavior的使用
原文地址:http://www.it610.com/article/4918541.htm 行为就是继承yii\base\behavior,可以绑定到任意yii\base\compent实例上,然后这 ...
- web图片使用
1. jpg.png.gif 适用场景 jpg 色彩丰富.大的图片例如 写实的图像,商品图片,人像,实物素材的广告banner等 png 色彩较少,有透明,或 具备较大亮度差异及强烈对比的图像,例如 ...
- 删除(注意,删除后,后面顶上去,所以id会一直变,所以我们用class来定义,因为id是唯一的)
删除de $(".delete").on("click",function(){ var id = $(this).attr("value" ...
- ExtJS学习之路第七步:contentEl与renderTo的区别
上回在Panel的应用中我们应该能大致区分开conteEl和renderTo,这回我们从定义中区分. 在Panel的API中, contentEl:String指定一个现有的HTML元素或者id作为此 ...