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. 内置对象(Math对象、Date对象、Array对象、String对象)常用属性和方法

    Math对象 Math 是一个内置对象, 它具有数学常数和函数的属性和方法.不是一个函数对象. 与其它全局对象不同的是, Math 不是一个构造函数.  Math 的所有属性和方法都是静态的. 跟数学 ...

  2. Ubuntu设置打不开排错

    记录一次系统设置打不开排错过程 系统信息: ××××××:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu ...

  3. redis单机上部署集群

    一.安装单机redis  redis的安装:版本至少是3.2.8及其以上,这里以3.2.8版本为例说明 1.安装redis wget http://download.redis.io/releases ...

  4. 登录案例version1 基本登录+验证码

    package com.frxx.web.servlet; import com.frxx.domain.User; import com.frxx.service.impl.UserServiceI ...

  5. mysql5.7日志时间戳(log_timestmaps)与系统时间不一致问题以及日志报Got an error reading communication packets情况分析

    一.mysql安装后error_log日志时间戳默认为UTC(如下图),因此会造成与系统时间不一致,与北京时间相差8个小时. 解决errro_logs时间戳与linux系统时间不一致问题 step1: ...

  6. mysql读取不同位置配置文件顺序

    读取顺序为: /etc/my.cnf basedir/my.cnf datadir/my.cnf --defaults-extra-file    #在读取全局配置文件之后,读取用户配置文件(~/.m ...

  7. easyui的datagrid用js插入数据等编辑功能的实现

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. php:php相关的函数或用法记录

    //1:判断字符串是否全是字母组成的 $str = 'AAKAaa_aLJIGF'; var_dump(ctype_alpha($str)); //boolean false,全部是英文时才是返回tr ...

  9. jQuery背景插件backstretch使用指南

    http://www.bkjia.com/Javascript/987917.html

  10. C#里边的控件缩写大全(比较规范)

    标准控件1 btn Button 2 chk CheckBox 3 ckl CheckedListBox 4 cmb ComboBox 5 dtp DateTimePicker 6 lbl Label ...