[LeetCode]60. Permutation Sequence求全排列第k个
/*
n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数
*/
public String getPermutation(int n, int k) {
k--;
List<Integer> list = new ArrayList<>();
StringBuilder res = new StringBuilder();
int count =1;
//以每个数字开头的集合有多少中排列
for (int i = 2; i <= n -1; i++) {
count*=i;
}
//记录哪些数字还没用
for (int i = 1; i <=n ; i++) {
list.add(i);
}
//回合数,也就是小集合的size
int round = n-1;
while (round>=0)
{
int num = list.get(k/count);
res.append(num);
list.remove(k/count);
if (round>0)
{
k = k%count;
count/=round;
}
round--;
}
return res.toString();
}
[LeetCode]60. Permutation Sequence求全排列第k个的更多相关文章
- 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,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [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 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(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 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 ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
随机推荐
- 利用python库stats进行t检验
t检验通常分为三种,分别是单样本t检验.双样本t检验和配对样本t检验.本文基于python的scipy.stats函数对每种t检验进行了介绍和实验. 一.t检验介绍 无论哪种t检验,都有以下的基本 ...
- LeetCode 048 Rotate Image
题目要求:Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 deg ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- 第一次个人作业 - 软件工程与UML
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...
- 单体->集群->模块化->分布式微服务
开头语: 每篇一段开头语,在技术的道路中寻找文采的乐趣.(如果随笔中都是大白话勿喷,兄弟姐妹们) 单体项目 单体项目适用于小型开发,或自己来进行小项目的测试和使用. 单体项目的缺憾 多人开发项目所出现 ...
- day7(redis的pipline使用)
1.pipeline原理 redis基本语法:https://www.cnblogs.com/xiaonq/p/7919111.html redis四篇:https://www.cnblogs.com ...
- MDX非常规百分比算法-过滤数据后的百分比
网上有很多关于占比的帖子,基本上都是按照层次结构来做的,比如某个子项占总体的百分比(\all).某个子项占父项的百分比(\parent).某个子项占其祖先的百分比(\ancestor)....等等,如 ...
- COMMENT SQL二次注入
这题目太顶了进去随便发个贴,出现登录已经提示用户名和密码了,弱密码登录(得自己去爆破)zhangwei666即可 没啥思路,扫下目录试试,kali的dirb扫到.git泄露githacker得到源码看 ...
- 嘶吼CTF easy calc
进入之后可以看到我们需要输入一个计算式来得到答案,burpsuite进行抓包之后发现页面来自于calc.php 我们直接访问calc.php页面 发现源代码泄露 可以看到当我们没有输入num值的时候就 ...
- ActionResult的返回类型
ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为 ...