060 Permutation Sequence 排列序列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
按大小顺序列出所有排列情况,并一一标记,
可得到如下序列 (例如, n = 3):
1."123"
2. "132"
3. "213"
4. "231"
5. "312"
6. "321"
给定 n 和 k,返回第 k 个排列序列。
注意:n 介于1到9之间(包括9)。
详见:https://leetcode.com/problems/permutation-sequence/description/
Java实现:
在数列 1,2,3,... , n构建的全排列中,返回第k个排列。
对于n个数可以有n!种排列;那么n-1个数就有(n-1)!种排列。
那么对于n位数来说,如果除去最高位不看,后面的n-1位就有 (n-1)!种排列。
所以,还是对于n位数来说,每一个不同的最高位数,后面可以拼接(n-1)!种排列。
所以可以看成是按照每组(n-1)!个这样分组。
利用k/(n-1)!可以取得最高位在数列中的index。这样第k个排列的最高位就能从数列中的index位取得,此时还要把这个数从数列中删除。
然后,新的k就可以有k%(n-1)!获得。循环n次即可。同时,为了可以跟数组坐标对其,令k先--。
class Solution {
public String getPermutation(int n, int k) {
k--;//to transfer it as begin from 0 rather than 1
List<Integer> numList = new ArrayList<Integer>();
for(int i = 1; i<= n; i++){
numList.add(i);
}
int fac = 1;
for(int i = 2; i < n; i++) {
fac *= i;
}
StringBuilder res = new StringBuilder();
int times = n-1;
while(times>=0){
int indexInList = k/fac;
res.append(numList.get(indexInList));
numList.remove(indexInList);
k = k%fac;//new k for next turn
if(times!=0){
fac = fac/times;//new (n-1)!
}
times--;
}
return res.toString();
}
}
参考:https://www.cnblogs.com/springfor/p/3896201.html
060 Permutation Sequence 排列序列的更多相关文章
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- 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 p ...
- 【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] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [Swift]LeetCode60. 第k个排列 | Permutation Sequence
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- 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 ...
- 60. Permutation Sequence(求全排列的第k个排列)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- MySQL条件判断处理函数_20160925
MySQL条件判断处理 一.假如我想把salesperson 分成 5组,计算每个销售分组的业绩 首先先将销售分组 SELECT *, CASE WHEN salesperson IN (" ...
- CSS 浏览器兼容
1. 兼容 IF <!--[if lte IE 7]> <style type="text/css"> .menu { position:relative ...
- ExecutorService和CompletionService区别
ExecutorService和CompletionService区别: ExecutorService:一直习惯自己维护一个list保存submit的callable task所返回的Future对 ...
- 用c++STL实现进程管理
项目要求: 设计一个允许n个进程并发运行的进程管理模拟系统.该系统包括有简单的进程控制,其进程调度采用时间片轮转算法.每个进程用一个PCB表示,其内容根据具体情况设置.各进程之间有一定的同步关系(可选 ...
- 4-数组、指针与字符串1.3-this指针
this指针时一个隐含于每一个类的成员函数中的特殊指针(包括构造函数和析构函数),它用于指向正在被成员函数操作的对象. this指针明确地指出了成员函数当前所操作的数据所属的对象.实际过程是,当通过一 ...
- AngularJS 2.0 学习
前提: 下载和安装node.js 和 npm. https://nodejs.org/en/download/ npm安装 需要自己google 安装好之后,可以在cmd窗口中查看安装的版本 node ...
- JAVA学习笔记——(一)
今日内容介绍 1.Java开发环境搭建 2.HelloWorld案例 3.注释.关键字.标识符 4.数据(数据类型.常量) 01java语言概述 * A: java语言概述 * a: Java是sun ...
- Gym - 101889E Enigma(数位填数+记忆化)
https://cn.vjudge.net/problem/Gym-101889E 1??????????????????????????????? 2 10000000000000000000000 ...
- HDU - 6415 多校9 Rikka with Nash Equilibrium(纳什均衡+记忆化搜索/dp)
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...