LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第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 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.
Note: Given n will be between 1 and 9 inclusive.
简要概括内容 :寻找到给定n的集合(n = 3 ,[1,2,3]),寻找它的kth个全排列子列。
分析:
方法一: 利用STL中的next_permutation函数实现
特点:代码简洁,暴力枚举
代码:
string getPermutationEx(int n, int k)
{
string s(n, '0');
for (int i = 0; i < n; ++i)
{
s[i] = i + 1;
}
for (int i = 0; i < k - 1; ++i)
{
next_permutation(s.begin(), s.end());
}
return s;
}
string getPermutation(int n, int k)
{
string strTemp(n, '0');
string strRes;
for (int i = 0; i < n; ++i)
{
strTemp[i] += i + 1;
}
int nNum = 1;
int nTemp = n;
while (0 != --nTemp)
{
nNum *= nTemp;
}
int kTemp = k - 1;
int nA;
nTemp = n - 1;
for (auto iterBg = strTemp.begin(); iterBg != strTemp.end();)
{
nA = kTemp / nNum; // a = k / (n - 1)!;
kTemp = kTemp % nNum; // k = k % (n - 1)!;
strRes.push_back(strTemp[nA]);
strTemp.erase(iterBg + nA);
nNum = nNum / (nTemp ? nTemp : 1);
--nTemp;
}
return strRes;
}
/***********************2017年5月3日更新*******************************************/
测试代码:
// test for Permutation Sequence
int main()
{
string s = getPermutation(1, 1);
for (int i = 0; i < s.size(); ++i)
{
printf("%c", s[i]);
}
}
备注:
此处对康托逆展开做一个说明:
为了寻找到 n = 5 , k = 6的子序列步骤如下:
- n = 5,则说明初始序列为“12345”,使用a1、a2、a3、a4、a5表示;
- 根据康托逆展开中描述,该序列变化了k = k - 1 = 5次,即在 “12345“的第五个序列
- a1 = k / (n - 1)! = 5 / 4! = 0; // 整除 第一位数字为比“1”大0的数字“1”
- k1 = k % (n - 1)! = 5 % 4! = 5; // k1 作为下一次运算的k 带入算式
- a2 = k1 / (n - 2)! = 5 / 3! = 0; //整除 第二位数字为比“2”大0的数字,“1” 已经被“取”走所以此处取“2”
- k2 = k1 % (n - 2)! = 5 % 3! = 5; // k2 作为下一次运算的k 带入算式
- a3 = k2 / (n - 3)! = 5 / 2! = 2; //整除 第三位数字为比“3”大2的数字,(“1”“2” 已经被“取”走)此处取“5”
- k3 = k2 % (n - 3)! = 5 % 2! = 1; // k3 作为下一次运算的k 带入算式
- a4 = k2 % (n - 4)! = 1 / 1 = 1; // 第四位数字为比“3”大1的数字,(“1”“2” 已经被“取”走)此处取“4”
- k4 = k3 % (n - 4)! = 5 % 2! = 0; // k3 作为下一次运算的k 带入算式
- a5 为剩余的 “3”;// 当然程序设计的时候只需要对 (n - i)!进行非0处理就可以了,不需要单独进行循环外处理。
算法逻辑:
- 通过n,k创建初始化的 strTemp;
- 开始寻找第K序列;// 第k = k - 1个
- 寻找a1 ; // a = k / (n - 1)!;
- k = k % (n - 1)!;
- 将a存入 输出数据strRes中;
- 移除str[a1]元素
重复上述。
谢谢小伙伴提的意见,后续博客会更新leetcode相关内容。本来不打算写下来的,毕竟leetcode题目博客在网上一大抄,但是个人还是觉得吸取大家的意见,顺路巩固加强下自己的理解,好记性不如烂笔头!
LeetCode:60. Permutation Sequence,n全排列的第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(康托展开)
描述: 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: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
随机推荐
- 初步认识Thymeleaf:简单表达式和标签。(一)
本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助. 对于Thymeleaf,网上特别官方的解释无非就是:网站或者独立应用程序 ...
- 微信小程序已经开放个人开发者申请了,还不快上车?
前言 就在昨天(3月27号),微信公众号平台推送了文章"小程序新能力",这篇文章是广大开发者的福音.个人开发者可申请小程序!!! 小程序开放个人开发者申请注册,个人用户可访问微信公 ...
- DPDK QoS之分层调度器
原创翻译,转载请注明出处. 分层调度器的时机主要体现在TX侧,正好在传递报文之前.它的主要目的是在每个网络节点按照服务级别协议来对不同的流量分类和对不同的用户的报文区分优先级并排序. 一.概述分层调度 ...
- jemeter正则表达式
- C#中关于WebBrowser的一些细节设置
在winform中有一个控件可以显示html的内容,该控件就是webbrowser,设置它的DocumenText属性为HTML的内容即可. 在使用WebBrowser做UI的时候,我们有时不希望里面 ...
- 回答: 2017-03-19的关于css+div布局的疑问 2017-04-05
原问题为红色,回答为黑色 第一次布局一个静态网页,发现许多细节都需要自己探索,出现诸如以下问题: 1.布局问题:经常出现一个div被挤出来?做到一半少一个div布局? 布局之前,要点打好框架,明确每个 ...
- 深入学习 DUBBO
1.什么是 RPC 协议? RPC 的全称是 Remote Procedure Call 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显 ...
- C#非泛型集合和泛型集合的超级详解
C# 泛型集合之非泛型集合类与泛型集合类的对应: ArrayList对应List HashTable对应Dictionary Queue对应Queue Stack对应Stack SortedList对 ...
- hive的安装和升级
目录 [toc] 安装 升级 安装 nohup hive –service hiveserver & 启动: sudo nohup ./hive --service metastore &am ...
- 设计模式(三)—代理模式
目录: 一.概述 二.静态代理 三.动态代理 四.静态代理和动态代理的区别 一.概述 代理模式就是多一个代理类出来,替原对象进行一些操作,比如我们在租房子的时候回去找中介,为什么呢?因为你对 ...