class Solution {
public:
string getPermutation(int n, int k) {
string ans(n, '');
vector<bool> flag(n, false);
int count = ;
for (int i = ; i < n; i++)
count *= i;
int m;
int m_f;
int index = ;
k = k - ;
while (index <n-)
{
m = k / count;
m_f = ;
for (int i = ; i <= m; i++, m_f++)
{
while (flag[m_f])
m_f++;
}
m_f--;
flag[m_f] = true;
ans[index] = m_f + + '';
index++;
k = k % count;
count = count / (n - index );
}
int i = ;
while (flag[i])
i++;
ans[n - ] = i + + '';
return ans; }
};

总结:几乎是用找规律算出来的,再次写,未必能流畅写下来。

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

LEETCODE60——第K个排列的更多相关文章

  1. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

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

  2. LeetCode60. 第k个排列

    解法一:用next_permutation()函数,要求第k个排列,就从"123...n"开始调用 k - 1 次 next_permutation()函数即可. class So ...

  3. Leetcode60. Permutation Sequence第k个排列

    给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  4. LeetCode 笔记21 生成第k个排列

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

  5. LinkCode 第k个排列

    http://www.lintcode.com/zh-cn/problem/permutation-sequence/# 原题 给定 n 和 k,求123..n组成的排列中的第 k 个排列. 注意事项 ...

  6. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  7. LeetCode 60 第K个排列

    题目: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "13 ...

  8. LeetCode(60): 第k个排列

    Medium! 题目描述: 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" ...

  9. LeetCode 中级 - 第k个排列(60)

    可以用数学的方法来解, 因为数字都是从1开始的连续自然数, 排列出现的次序可以推 算出来, 对于n=4, k=15 找到k=15排列的过程: 1 + 对2,3,4的全排列 (3!个) 2 + 对1,3 ...

随机推荐

  1. Request a certificate from a certificate vendor

    Request a certificate from a certificate vendor Now, with your CSR in hand, visit the Web site of yo ...

  2. transition动画最简使用方式

    HTML <a href="#" title="">test</a> CSS a {display:block; width:200px ...

  3. PAT甲级——1112 Stucked Keyboard (字符串+stl)

    此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078   1112 Stucked Keyboa ...

  4. HDU1409 Is It a Number

    http://acm.hdu.edu.cn/showproblem.php?pid=1409 没啥好说的,至今也不知道到底错在哪里了,看了discuss才过的 #include <iostrea ...

  5. 2017ACM/ICPC广西邀请赛 CS Course

    题意:删除指定数字,求剩下数字的与或非值 解法:保存一下前缀和后缀 #include <iostream> #include <stdio.h> #include <ve ...

  6. GIT使用笔记一:GIT初始化配置

    本人系统环境:centos6.5 下 LNMP centos下git安装很简单sudo yum install gitOK 可先进行git 的全局配置 用户信息 git config --global ...

  7. 《深入理解java虚拟机》笔记(3)实战:OutOfMemoryError异常

    一.Java堆溢出 测试代码: /** * <p>Java堆异常测试</p> * <code>VM Args: -Xms20m -Xmx20m -XX:+HeapD ...

  8. java实现xml文件读取并保存到对象

    首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...

  9. Redis hash(哈希)

    Redis hash可储存多个键值对,适合储存对象的属性. 1.hset key fieldName fileValue    //hset即hash set,set这里是设置的意思.往hash中添加 ...

  10. JS根据选择的日期计算年龄

    本例中用的是easyUI的datebox $('#cal_birthday').datebox({ onSelect: function(date){ //根据选则的日期计算年龄 //alert(da ...