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 ...
随机推荐
- shell 3数组
shell数组 shell支持一维数组(不支持多维数组),并且没有限定数组的大小. 定义数组 shell中,用括号来表示数组,数组元素用空格分隔,下标从0开始,元素的类型 方式1 数组名=(值1 值2 ...
- 大家都对vertical-align的各说各话
原文地址:http://www.blueidea.com/tech/web/2008/5892.asp 最近几天仔细研究了一下vertical-align这个属性,结果让我大吃一惊,这个很“资深”的C ...
- MariaDB管理系统
MariaDB管理系统 [root@c4kaichen@163 ~]# yum install mariadb[root@c4kaichen@163 ~]# yum install -y mariad ...
- ubuntu 查看系统是32位还是64位
查看cpu信息 cat /proc/cpiinfo 查看ubuntu版本: cat /etc/issue 查看系统是32位还是64位 方法1: #查看long的位数,返回32或64 getconf L ...
- ubuntu 16.04 LTS 安装 teamviewer 13
背景介绍 由于需要做现场的远程支持,经协商后在现场的服务器上安装TeamViewer 以便后续操作. 本来以为很简单的一件事,谁知却稍微费了一番周折 :( 记录下来,希望提醒自己的同时也希望能够帮到 ...
- 6.19-response(响应),session(会话技术,服务器端技术) 内置对象,application(内置对象),pageContext (内置对象),cookie(客户端技术)
一.response(响应) 页面重定向 response.sendRedirect(""); 转发: request.getRequestDispatcher("&qu ...
- python输出格式化及函数format
格式 描述%% 百分号标记%c 字符及其ASCII码%s 字符串%d 有符号整数(十进制)%u 无符号整数(十进制)%o 无符号整数(八进制)%x ...
- js中的数组操作
<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" conten ...
- 使用Python调用动态库
我个人在日常使用电脑时,经常需要使用Google,于是就要切换代理,基本上是一会儿切换为代理,一会儿切换成直连,老是打开internet 选项去设置,很不方便,于是我萌生了一个想法: 做一个开关,我想 ...
- Java调用Bat
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Input ...