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 ...
随机推荐
- 在线上服务器上无管理员权限下升级NodeJS版本
前言 最近发现一个线上机器的问题,是因为node版本过低导致的,线上机器的node版本还是0.x版,遂打算升级node版本. 但是发现常规的npm包的n模块无法使用,提示没有权限创建文件夹,导致nod ...
- OSPF相关知识与实例配置【第一部分】
OSPF相关知识与实例配置[基本知识及多区域配置] OSPF(开放式最短路径优先协议)是一个基于链路状态的IGP,相比于RIP有无环路:收敛快:扩展性好等优点,也是现在用的最多的:所以这次实验就针对于 ...
- iPhone与iWatch连接、控制、数据传递(Swift)
最近在做一个项目,涉及到iPhone设备和手表传输数据.控制彼此界面跳转,在网上找了很多资料,发现国内的网站这方面介绍的不多,而国外的网站写的也不是很全,所以在这写这篇博客,给大家参考一下,望大神指点 ...
- STM32驱动OV7725摄像头颜色识别
实验目的: 使用stm32驱动OV7725摄像头进行图像实时采集,在tft屏幕上实时显示并识别图像中的特定颜色,在颜色的周围画上框. 实验现象: 我的工程代码链接: http://download.c ...
- 20155237 2016-2017-2 《Java程序设计》第5周学习总结
20155237 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try...catch 与C语言中程序流程和错误处理混在一起不同,Jav ...
- linux XAMPP安装与使用
linux安装 下载好后考至目录, chmod +x 接安装包 在执行 ./ 接安装包 启动 XAMPP /opt/lampp/lampp start 停止 XAMPP /opt/lampp/lam ...
- Struts2基础学习(三)—Result和数据封装
一.Result Action处理完用户请求后,将返回一个普通的字符串,整个普通字符串就是一个逻辑视图名,Struts2根据逻辑视图名,决定响应哪个结果,处理结果使用<result&g ...
- hibernate的反转引擎生成两个实体类的问题
在使用myeclipse中自带的hibernate 进行jsp开发时候遇到了这个问题.使用hibernate的反转引擎从数据库生成生成实体类,一个表生成了两个类,xx.java和xxId.java . ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合
日常啰嗦 本来这一篇和接下来的几篇是打算讲一下JDBC和数据库优化的,但是最近很多朋友加我好友也讨论了一些问题,我发现大家似乎都是拿这个项目作为练手项目,作为脚手架来用的,因此呢,改变了一下思路,JD ...
- Android -- 从源码带你从EventBus2.0飚到EventBus3.0(一)
1,最近看了不少的面试题,不管是百度.网易.阿里的面试题,都会问到EventBus源码和RxJava源码,而自己只是在项目中使用过,却没有去用心的了解它底层是怎么实现的,所以今天就和大家一起来学习学习 ...