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. angularJs 指令调用父controller某个方法

    1.父级controller:例如有个 init() 方法; 父级与子级的通信数据是$scope.controlFlag={}; 那么可以在父级controller里这样写:$scope.contro ...

  2. atom通过remote ftp同步本地文件到远程主机的方法

    视频教程:https://ninghao.net/video/3991 搜索 “remote ftp”, 点击 “Package”搜索包,Install”安装 本地打开需要同步的项目目录 创建 rem ...

  3. 【手撸一个ORM】第九步、orm默认配置类 MyDbConfiguration,一次配置,简化实例化流程

    这个实现比较简单,事实上可配置的项目很多,如有需要,请读者自行扩展 using System; namespace MyOrm { public class MyDbConfiguration { p ...

  4. 如何使用在Windows 下AspNetCore Api 和 consul

    在Windows 下如何使用 AspNetCore Api 和 consul https://blog.csdn.net/sD7O95O/article/details/80750803 一.概念:什 ...

  5. OAuthLogin2.0

    开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台   Nuget地址:https://www.nuget.org/packages/OAuthLogin ...

  6. cmd下一些常用的命令

    systeminfo   列出主机名,内存等一些系统信息 tasklist 显示进程信息      (tasklist | finstr eclipse)

  7. ubuntu中安装vsftp

    1.安装 $ sudo apt-get install vsftpd 2. 配置 需要配置文件/etc/vsftpd.conf 来进行设置参数以对FTP进行控制,包括访问权限,流量等进行控制. 执行s ...

  8. P4869 罪犯分组

    思路: 明显的dp,虽然我想到了二进制模拟,想到了转移,但还是先看了题解,原来真是这样,,,,不是第三题吗? 用f[i]表示,对于前i个罪犯最少需要分几组. 对于每个状态用二进制表示,第i位上1,0表 ...

  9. css3响应式图片

    响应式图片指用户代理根据输出设备的分辨率不同加载不同类型的图片,不会造成带宽的浪费. 同时,在改变输出设备类型或分辨率时,能及时加载对应类型的图片.   常用的实现方式: 1.用srcset和size ...

  10. cocos2dx贝塞尔曲线--使用PS辅助规划动作路径

    bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ...