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.

Solutions: Caculates the index of the first number by  idx = (k -1) / (n-1)! ,  and the new k and new n, then goes to next round.

     string getPermutation(int n, int k) {
vector<int> nums;
vector<int> factors(,);
for(int i = ; i <= n; i++){
nums.push_back(i);
factors.push_back(factors[i - ] * i);
} string ret;
while(k > && nums.size() > ) {
int idx = (k -) / factors[n -];
ret.push_back(nums[idx] + );
nums.erase(nums.begin() + idx);
k = k - idx * factors[n - ];
n --;
} return ret;
}

Permutation Sequence [LeetCode]的更多相关文章

  1. Permutation Sequence leetcode java

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

  2. [LeetCode] 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] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

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

  5. 【LeetCode练习题】Permutation Sequence

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

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

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

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

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

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

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

  9. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

随机推荐

  1. ServiceStack.OrmLite 笔记4 删

    删除 db.DeleteAll(); //各种姿势 db.Delete(p => p.Age == 27);// db.Delete(q => q.Where(p => p.Age ...

  2. php 连接mysql的问题

    当前我的情况是安装上mysql配置了my.cnf中的socket,并且在指定位置也生成了 mysql.sock 的sock文件,此时我通过php.ini来设置php连接mysql.sock的文件,但是 ...

  3. C#TextBox自动滚动到最低端

    C#中一个RichTextBox,当新写入内容时,超过当前显示区域后,自动滚动到最低端,方便查看内容. private void txtInfo_TextChanged(object sender,  ...

  4. CSS笔记(一)CSS规则

    CSS是层叠式样式表(Cascading Style Sheets)的缩写,定义了如何显示HTML元素. CSS规则由两个主要的部分构成:选择器 + 一条或多条声明. 每条声明由一个属性和一个值构成. ...

  5. Zeller公式推导及C#代码示例(待完善)

    Zeller公式用于计算给定日期是星期几. 该方法可以用数论知识进行证明. 假设给定日期Date为Year-Month-Day,求解该日期是星期几的问题实际上就是以之前某个确定星期几的日期作为参考点, ...

  6. Android学习参考2

    一名自学成才的Android开发者怒答! 1. Google做开发前完全是小白,真心不知道有Google这东西,只晓得百 度,遇到问题直接百度,不是黑百度,百度在娱乐八卦方面确实靠谱,但是技术方面查出 ...

  7. Linux_文档编辑器_简介

    1. vi 2. vim 3. ubuntu 有一个 自己的图形化的 文档编辑器,用起来比较方便: gedit 4. 5.

  8. mysql 逻辑架构

    1.mysql是基于网络的客户端/服务器架构,服务器上层是连接线程,解析器,查询缓存,下层是存储引擎. 2.每个客户端连接,服务器都有一个对应的线程,这个线程只为这个连接查询服务,高版本的mysql支 ...

  9. DDL之操作表

    DDL之操作表 DDL是数据定义语言,用来定义数据库对象:数据库.表.列等.其中定义数据库我们已经在DDL之操作数据库中详细讲解了,今天我们来学习使用DDL操作表. 1.创建表 使用数据定义语言创建表 ...

  10. linux rwxrwxrwt文件夹属性

    /tmp 的permission是rwxrwxrwt chmod 0777 /abc       rwxrwxrwx chmod  777 /abc        rwxrwxrwx chmod 17 ...