Question

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.

Solution -- Math

We can conclude a pattern by obeservation.

[x1, x2, x3, .., xn]

If we fix x1, then number of permutations is (n - 1)!

If we fix xand x2, then number of permutations is (n - 2)!

Following this thinking way, we can solve this problem by finding xi iteratively.

We also need a visited array here to note which element has been used.

 public class Solution {
public String getPermutation(int n, int k) {
StringBuilder sb = new StringBuilder(n);
if (n < 1 || n > 9)
return sb.toString();
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
if (k > factorial || k < 1)
return sb.toString();
boolean[] visited = new boolean[n]; int num = n;
int start = 1;
while (num > 0) {
factorial /= num;
start += (k / factorial);
if (k % factorial == 0)
start -= 1;
int index = 0;
for (int i = 1; i <= n; i++) {
// Find the right index
if (!visited[i - 1])
index++;
if (index == start) {
sb.append(i);
visited[i - 1] = true;
break;
}
}
k = k - (start - 1) * factorial;
start = 1;
num--;
}
return sb.toString();
}
}

Permutation Sequence 解答的更多相关文章

  1. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  2. LeetCode OJ 60. Permutation Sequence

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

  3. LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence

    1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. 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 序列排序

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

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

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

  8. 60. Permutation Sequence

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

  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. RedHat9通过Host-only配置网络连接

    首先我用的是VMware8版本安装的RedHat9.VMware给我们提供了三种让虚拟机里的安装系统连上网的方式.分别是Host-only,Bridge,NAT.我要讲的是Host-only. 第一步 ...

  2. array模块

    array模块定义了一种序列数据结构,看起来和list很相似,但是所有成员必须是相同基本类型. 2.1 array-固定类型数据序列 array作用是高效管理固定类型数值数据的序列. 2.2.1 初始 ...

  3. Angular Textarea 高度自动变化

    很多前端开发的朋友可能都会遇到textarea 输入框的高度不能自动随着用户的输入变化的问题,今儿小生也遇到了, 并通过网络上的信息解决了这个问题,于是将解决方法贴上,以作备忘. directiveA ...

  4. NTP配置实践

    前言 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.不管是平时使用的私人计算机还是在工作中搭建的服务器集群.时间的统一性和准确性是十分 ...

  5. Oracle varchar 字段排序问题

    数据库字段: 想要的结果: 实际查询的结果: 解决方法:使用CAST函数把varchar2转为int类型 order by CAST(CODE AS INTEGER)

  6. 使用Reporting Service订阅对域外用户发邮件

    默认情况下使用Reporting Service对域外邮件发送会失败,一般可能会碰到下面的两个错误: ERROR 1: Subscription Error: "The e-mail add ...

  7. qemu-kvm-1.1.0源代码中关于迁移的代码分析

    这篇文档基于qemu-kvm-1.1.0源代码进行分析. 首先,源代码中的hmp-commands.hx文件里有下面内容: { .name = "migrate",/* 在moni ...

  8. feel倍儿爽

    今天装的360,在卸载就要权限,在自己的电脑卸载360还要权限,真是一物降一物,安装了个牛逼的卸载软件就卸载了

  9. Ubuntu eclipse 命令补全失效 (转载)

    我的eclipse 3.4,从ibm网站上下载解压后使用.发觉自动补全功能(alt + /)失效. 解决的办法: 1.(eclipse)window --> preferences --> ...

  10. CDZSC_2015寒假新人(1)——基础 g

    Description Ignatius likes to write words in reverse way. Given a single line of text which is writt ...