60. Permutation Sequence (String; Math)
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.
思路:
本题不像Permutation,不要求罗列所有permutation,所以没必要用回溯递归。
本题如果用Next Permutation,会导致超时。需要寻找规律。
规律:在n!个排列中,除去第一位,后几位共有(n-1)!个排列,所以第一位的元素总是(n-1)!一组出现的。那么,第k行第一位的值就=nums[(k-1)/(n-1)!]。
依次类推第二位的值=nums[(k2-1)/(n-2)!],其中k2=(k-1)%(n-1)! Note: +1是因为k从1开始计数,而非0
class Solution {
public:
string getPermutation(int n, int k) {
string result = "";
bool hasUsed[n+]; //whether the number has used in the string
for(int i = ; i<=n; i++)
{
hasUsed[i] = false;
}
int dp[n]; //permutation number of a digit with i width = i!
dp[] = ;
dp[] = ;
for(int i = ; i< n; i++)
{
dp[i] = i* dp[i-]; //用动态规划法求阶乘
}
int num; //记录第i位的数字
stringstream ss;
string str;
for(int i = n; i>; i--)
{
num = k/dp[i-];
if(k%dp[i-] != ) num+=;
int counter = ;
int j;
for(j = ; j<=n && counter!=num; j++)
{
if(!hasUsed[j])counter++; //如果这个数字已经出现,则不计数
if(counter == num) break;
}
hasUsed[j] = true;
ss.clear();
ss<<j;
ss>>str;
result += str;
k = k-(num-)*dp[i-]; //该位的数字订了后,也就意味着已经处理一部分permutation number, 要把这部分去掉
}
return result;
}
};
思路II:
首先,不用把每个状态阶乘都存储,可以求出n! 然后每次遍历除以i便可以得到当前循环所需要的阶乘。
其次,不需要flag,但设一个num[]记录还剩余的数字,这样的好处是,原先要做加法、赋值、比较操作,现在update num只需要赋值操作。
class Solution {
public:
string getPermutation(int n, int k) {
string result = "";
vector<int> num; //num记录第i个数字是多少
int factorial = ;
for(int i = ; i < n; i++){
num.push_back(i+); //给num赋初值
factorial *= num[i]; //求阶乘(n-1)!
}
int index; //记录第i位的数字在num[]中的下标
for(int i = n; i > ; i--) //遍历每一位数字
{
factorial /= i; //update factorial
index = (k-)/factorial; //第n位数以(n-1)!为一组,按组出线;index表示第k行(从1开始计数)在第几组(从0开始计数)
k = (k-)%factorial +; //update k
result += (''+num[index]);
for(int j = index; j+ < i; j++) //update num
{
num[j] = num[j+];
};
}
return result;
}
};
60. Permutation Sequence (String; Math)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 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 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 60. Permutation Sequence (JAVA)
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- 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 ...
- 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 t ...
- 【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.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
随机推荐
- windows任务计划程序 坑
- cpu监控之三:mpstat命令
mpstat是MultiProcessor Statistics的缩写,是实时系统监控工具.报告CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有CP ...
- Mysql-binlog的移动和归档
#!/bin/bash # To backup and archive binlogs. declare -i NUM=0 declare -i SUM=0 SUM=`/bin/ls -l mysql ...
- java.lang.IllegalStateException: Fragment bb{42261900} not attached to Activity
A.处理异常java.lang.IllegalStateException: Fragment bb{42261900} not attached to Activity处理方式:由于在线程中调用Fr ...
- php如何判断IP为有效IP地址
不需要正则表达式来判断,因为在php5.2.0之后,有专门的函数来做这个判断了. 判断是否是合法IP if(filter_var($ip, FILTER_VALIDATE_IP)) { // it's ...
- OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo)
OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo) 1.问题描述 [oracle@dou_ra ...
- CSS Web安全字体组合
常用的字体组合 font-family属性是多种字体的名称,作为一个"应变"制度,以确保浏览器/操作系统之间的最大兼容性.如果浏览器不支持的第一个字体,它尝试下一个的字体. 你想要 ...
- 0_Simple__UnifiedMemoryStreams
使用 OpenMP 和 pthreads 两种环境,利用实现统一内存编址,计算基本的矩阵乘法 result = α * A * x + β * result . ▶ 源代码 #include < ...
- CentOS 7.4 安装部署 iRedMail 邮件服务器
在公司部署了一套开源的邮件网关Scrollout F1用来测试,由于Scrollout F1需要使用IMAP协议连接到邮件服务器上的隔离邮箱,抓取GOOD和BAD文件夹里的邮件进行贝叶斯学习,但公司的 ...
- MySQL优化十大技巧
转自:https://m.2cto.com/database/201701/557910.html MYSQL优化主要分为以下四大方面: 设计:存储引擎,字段类型,范式与逆范式 功能:索引,缓存,分区 ...