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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "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)的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. [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 ...

  4. 60. Permutation Sequence (JAVA)

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  5. 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 ...

  6. 60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  7. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  8. 【LeetCode】60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  9. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. Neutron 理解 (2): 使用 Open vSwitch + VLAN 组网 [Neutron Open vSwitch + VLAN Virtual Network]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  2. VMware仅主机模式访问外网

    原文转载至:https://blog.csdn.net/eussi/article/details/79054622 保证VMware Network Adapter VMnet1是启用状态  将可以 ...

  3. Source引擎多人模式网络同步模型

    转自:http://gad.qq.com/program/translateview/7168875 Source引擎的多人游戏使用基于UDP通信的C/S架构.游戏以服务器逻辑作为世界权威,客户端和服 ...

  4. 第3章 文件I/O(8)_贯穿案例:构建标准IO函数库

    9. 贯穿案例:构建标准IO函数库 //mstdio.h #ifndef __MSTDIO_H__ #define __MSTDIO_H__ #include <unistd.h> #de ...

  5. solr 通过【配置、多值字段、动态字段】来解决文本表达式查询精确到句子的问题

    一.Solr Multivalue field属性positionIncrementGap理解 分类:Lucene 2014-01-22 10:39阅读(3596)评论(0) 参考:http://ro ...

  6. Python - Django - ORM 操作表

    ORM 的对应关系: 类        --->    数据库表对象     --->    数据库行属性     --->    字段 操作数据库表     --->     ...

  7. WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException

    Hadoop 2.7.4 The reason is this: originally, DataStreamer::closeResponder always prints a warning ab ...

  8. Java基本类型与运算

    问题及答案来源自<Java程序员面试笔试宝典>第四章 Java基础知识 4.4基本类型与运算 1.Java提供了哪些基本数据类型? Java一共提高了八种原始的数据类型:byte.shor ...

  9. python中__init__()、__new__()、__call__()、__del__()几个魔法方法的用法

    关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...

  10. mock单测

    mockMvc执行流程总结: 整个过程:1.mockMvc.perform执行一个请求:2.MockMvcRequestBuilders.get("/user/1")构造一个请求3 ...