题目大意:给出n和k,找到1..n这些数组成的有序全排列中的第k个。

首先,n的全排列可以分成n组,每一组由n-1个数组成。

例如  3的全排列,分成三组:

1  2  3  和 1  3  2

2  1  3  和 2  3  1

3  1  2  和 3  2  1

每一组的个数是(n-1)!,每一组的打头的都是i 。第i组以i打头。

先求 x = (k-1) / (n-1)!  + 1

比如  n = 3, k = 4.

x = 3/2 + 1 = 2,得到 第四个实际上第二组里面。

y = (k-1)%(n-1)! + 1 = 2,即,要求的东西在第二组的第二个数。

第二组的特征是,以2开头。所以我们找到第2个数,放到string ans的最高位置。

然后,求剩下的数里面的全排列的第y个。 即把2去掉,用1 和 3做全排列,取其中的第2个。

这时候,回到初始的步骤。递归即可。

垃圾的代码,现在脑子懵。。写的代码跟屎一样,完全没有逻辑啊!要优化!!

class Solution {
public:
int factorial(int n) //计算n的阶乘
{
if(n == || n == ) return ;
return n*factorial(n-);
} void back_track(vector<int>&used, string &ans, int n, int cnt, int k) //cnt表示ans里有几个元素。找到第k个。
{
if(cnt == n) return; //所有的都用完了 int fac = factorial(n--cnt); //初始是n-1 int x = (k-)/fac + ; //在第x 个序列 第一个数是第几个
k = (k-)%fac + ; //x序列的第k个 int i,j; i = ; while(used[i]) i ++; //找到第一个没用过的元素
for(j = ; j < x;i++) //这时候x指向的是第1个未使用的元素
{
if(!used[i]) j++; //此时x走过了j-1个元素。
}
while(used[i]) i++; //找到第x个未使用的元素 ans.append(,i + ''); //把这个元素放在头
used[i] = true; //这个元素被使用了
back_track(used, ans, n, cnt + ,k); //用剩下的去构造第k个元素
}
string getPermutation(int n, int k) {
int i;
vector<int> used(n+,false);
string ans;
back_track(used,ans,n,,k);
return ans;
} };

LeetCode 题解 Permutation Sequence 需要优化!的更多相关文章

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

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

  2. Java for LeetCode 060 Permutation Sequence

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

  3. [LeetCode 题解]: Permutation Sequcence

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

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

  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. 【leetcode】 Permutation Sequence (middle)

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

  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 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  9. 【Leetcode】Permutation Sequence

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

随机推荐

  1. 面试总结之PYTHON

    source code https://github.com/haoran119/interview/tree/master/interview%20summary%20of%20python [ZZ ...

  2. 解决Kubernetes 1.7.3 kube-apiserver频繁异常重启的问题(转)

    原文的帖子无法访问,我只能粘贴内容 近期将之前的一个用Kubernetes 1.3.7的环境更换为最新发布的用kubeadm安装的Kubernetes 1.6.4 Dashboard无法访问的问题&g ...

  3. MySQL 分区间进行数据展示 实例

    如何进行分区间数据统计示例 业务场景:统计消费总金额大于1000元的,800到1000元的,500到800元的,以及500元以下的人数. SELECT COUNT(CASE WHEN IFNULL(t ...

  4. Glow 效果材质

    转自:http://blog.csdn.net/panda1234lee/article/details/60960846 算法较简单,首先来看 Base color 部分: 就是将对事先准备好的三张 ...

  5. (error) MOVED 5798 172.17.0.3:6379

    登录没有启动集群模式(即缺少了那个"-c"): redis-cli -c -h yourhost -p yourpost

  6. Kafka参数详解

    一.相关参数配置 System 系统参数 #唯一标识在集群中的ID,要求是正数. broker.id=0 #服务端口,默认9092 port=9092 #监听地址,不设为所有地址 host.name= ...

  7. Install Greenplum OSS on Ubuntu

    About Greenplum Database Greenplum Database is an MPP SQL Database based on PostgreSQL.  Its used in ...

  8. Android 硬编码

    public class TextViewActivity extends Activity { // 声明TextView对象 private TextView textView; @Overrid ...

  9. sqoop导入导出

    sqoop产生背景 什么是sqoop sqoop的优势 sqoop1与sqoop2的比较 为什么选择sqoop1 sqoop在hadoop生态体系中的位置 sqoop基本架构 sqoop import ...

  10. 安装部署elasticsearch

    ELK下载:https://www.elastic.co/downloads/ Beats:负责收集系统数据,可以直接发送到es中,也可以通过logstash中转 logstash:收集日志,为bea ...