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.
分析
首先想到的是,采用STL库中的标准算法next_permutation(),寻找第k个排列即可,可是提交结果为Time Limited Exceed,显然,必须寻找更优算法。
借鉴了别人的思想,设计如下(原文博客):
数学解法
在n!个排列中,第一位的元素总是(n-1)!一组出现的,也就说如果p = k / (n-1)!,那么排列的最开始一个元素一定是nums[p]。
假设有n个元素,第K个permutation是
a1, a2, a3, ….. …, an
那么a1是哪一个数字呢?
那么这里,我们把a1去掉,那么剩下的permutation为
a2, a3, …. …. an, 共计n-1个元素。 n-1个元素共有(n-1)!组排列,那么这里就可以知道
设变量K1 = K
a1 = K1 / (n-1)!
同理,a2的值可以推导为
a2 = K2 / (n-2)!
K2 = K1 % (n-1)!
…….
a(n-1) = K(n-1) / 1!
K(n-1) = K(n-2) /2!
an = K(n-1)
STL标准算法实现(Time Limited Exceed)
class Solution {
public:
string getPermutation(int n, int k) {
if (n <= 0 || k <= 0 || k > factorial(n))
return "";
vector<int> v;
for (int i = 1; i <= n; i++)
v.push_back(i);
int index = 1;
while (index < k && next_permutation(v.begin(), v.end()))
index++;
string str = "";
for (int i = 0; i < n; i++)
str += IntToStr(v[i]);
str += '\0';
return str;
}
string IntToStr(int n)
{
string str = "";
while (n != 0)
{
str += (n % 10 + '0');
n /= 10;
}
reverse(str.begin(), str.end());
return str;
}
long factorial(int n)
{
if (n <= 1)
return 1;
else
return n * factorial(n - 1);
}
};
AC代码
class Solution {
public:
string getPermutation(int n, int k) {
vector<int> nums(n);
int pCount = 1;
for (int i = 0; i < n; ++i) {
nums[i] = i + 1;
pCount *= (i + 1);
}
k--;
string res = "";
for (int i = 0; i < n; i++) {
pCount = pCount / (n - i);
int selected = k / pCount;
res += ('0' + nums[selected]);
for (int j = selected; j < n - i - 1; j++)
nums[j] = nums[j + 1];
k = k % pCount;
}
return res;
}
};
LeetCode(60) Permutation Sequence的更多相关文章
- LeetCode(60): 第k个排列
Medium! 题目描述: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- Qt 学习之路 2(60):使用 DOM 处理 XML
Qt 学习之路 2(60):使用 DOM 处理 XML 豆子 2013年8月3日 Qt 学习之路 2 9条评论 DOM 是由 W3C 提出的一种处理 XML 文档的标准接口.Qt 实现了 DO ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- 设置Google搜索在新的标签页打开
Google搜索的结果,默认情况下点击进入是在本标签页打开的,这样就很麻烦, 可以在搜索结果的页面中进行设置,让它在新的标签页显示 搜索结果设置->搜索设置->新的标签页打开
- mysql远程备份
相关链接:https://blog.csdn.net/LiuHuan_study/article/details/81512831https://www.cnblogs.com/ryanzheng/p ...
- PhpStorm Xdebug远程调试环境搭建原理分析及问题排查
2017年05月26日 经验心得 目录 一. 环境介绍 二. 远程环境配置 2.2 Xdebug安装 2.3 配置 三. 本地phpstorm配置 3.1 下载远程代码 3.2 添加php解释器 ...
- javascript---DOM大编程
编程练习 制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. 点 ...
- Ubuntu下如何用命令运行deb安装包
转载自 WindTaiL的博客 如果ubuntu要安装新软件,已有deb安装包(例如:iptux.deb),但是无法登录到桌面环境.那该怎么安装?答案是:使用dpkg命令. dpkg命令常用格式如下: ...
- Oracle10g安装过程中ORA-27125问题解决
Oracle10g在CentOS7的安装过程中报错如下错误信息: ORA-: unable to create shared memory segment 解决办法: [root@dbsrv3 dat ...
- sed与正则表达式
行的开头(^) ^匹配每一行的开头 [root@sishen ~]# sed -n '/^103/ p ' employee.txt 103,Raj Reddy,Sysadmin 只有^出现在正则表达 ...
- 移动端UI自动化Appium测试——获取APK的Package及Activity属性值
1.如果有代码环境,直接在AndroidManifest.xml中查找: package值: Activity值: 2.如果没有开发代码,直接用命令获取: cmd进入到 android-sdk-win ...
- php debug/phpstorm调试
apache+phpstorm调试php代码,修改php.ini配置文件开启调试,没有以下代码加上即可, [XDebug]zend_extension="C:\php\php-7.0.12- ...
- Mac下部署与启动STF
一.stf在Mac下的部署1.安装Java及jdk可自己谷歌(如果不能自建云梯)2.安装nodejs包(我是直接在官网下载的LTS版本) • Node.js v8.12.0 to /usr/local ...