LEETCODE60——第K个排列
class Solution {
public:
string getPermutation(int n, int k) {
string ans(n, '');
vector<bool> flag(n, false);
int count = ;
for (int i = ; i < n; i++)
count *= i;
int m;
int m_f;
int index = ;
k = k - ;
while (index <n-)
{
m = k / count;
m_f = ;
for (int i = ; i <= m; i++, m_f++)
{
while (flag[m_f])
m_f++;
}
m_f--;
flag[m_f] = true;
ans[index] = m_f + + '';
index++;
k = k % count;
count = count / (n - index );
}
int i = ;
while (flag[i])
i++;
ans[n - ] = i + + '';
return ans; }
};
总结:几乎是用找规律算出来的,再次写,未必能流畅写下来。
"123"
"132"
"213"
"231"
"312"
"321"
LEETCODE60——第K个排列的更多相关文章
- [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 ...
- LeetCode60. 第k个排列
解法一:用next_permutation()函数,要求第k个排列,就从"123...n"开始调用 k - 1 次 next_permutation()函数即可. class So ...
- Leetcode60. Permutation Sequence第k个排列
给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- LeetCode 笔记21 生成第k个排列
题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...
- LinkCode 第k个排列
http://www.lintcode.com/zh-cn/problem/permutation-sequence/# 原题 给定 n 和 k,求123..n组成的排列中的第 k 个排列. 注意事项 ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- LeetCode 60 第K个排列
题目: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "13 ...
- LeetCode(60): 第k个排列
Medium! 题目描述: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" ...
- LeetCode 中级 - 第k个排列(60)
可以用数学的方法来解, 因为数字都是从1开始的连续自然数, 排列出现的次序可以推 算出来, 对于n=4, k=15 找到k=15排列的过程: 1 + 对2,3,4的全排列 (3!个) 2 + 对1,3 ...
随机推荐
- Jquery基本教程(背还是要背的)
Jquery入门学习 一.简介 1.Jquery是基于JavaScript的一种框架,兼容主流浏览器,提供了dom,animate(JQ+CSS),ajax; 2.Jquery2.0后版本不支持IE6 ...
- 源码分析(一) 进程cleos的命令解析
EOS版本:4.0 一.进程cleos的作用 cleos,即为client eos.从名字就可以猜出来,它是一个标准的客户端程序,而实际上,它也确实为一个标准的client^_^ 准确地说 ...
- js new Date("2016-07-01 08:00:00") 格式在IE内核浏览器中显示NaN的问题
js new Date("2016-07-01 08:00:00") 格式在IE内核浏览器中显示NaN的问题 废话就不多了,var dd = new Date("2016 ...
- Java获取上周,本周,本月,本年,开始结束时间 。日期工具类
由于获取日期经常会使用到,所有我自己写了一个工具类 1.基本上能用上的都写出来了,包括:1)获取当天的开始时间 2)获取当天的结束时间 3)获取昨天的开始时间 4)获取昨天的结束时间 5)获取明天的开 ...
- Java - 一道关于整型和字符类型相加的题目
题目 public class Test { public static void main(final String[] args) { final int a = 10; final int b ...
- C# 多线程程序隐患
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- NETCORE MVC模块化
NETCORE MVC模块化 ASP.NETCORE MVC模块化编程 前言 记得上一篇博客中跟大家分享的是基于ASP.NETMVC5,实际也就是基于NETFRAMEWORK平台实现的这么一个轻量级插 ...
- AWR实战分析之----direct path read temp
http://blog.sina.com.cn/s/blog_61cd89f60102eej1.html 1.direct path read temp select TOTAL_BLOCKS,USE ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest A. Toda 2 贪心 + 暴力
A. Toda 2 time limit per test 2 seconds memory limit per test 512 megabytes input standard input out ...
- jeesite框架搭建中mysql数据库导入问题
在进行mysql8.0的安装配置时,可以说是道路坎坷,之前介绍了如何安装配置mysql8.0,虽然mysql在正常情况下是很容易安装的,但是如果遇到特殊情况的时候那就需要特殊的处理. 这次我遇到的问题 ...