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. MongoDB GUI管理工具Mongo VUE

    一.前言 现在越来越多的公司开始采用非关系数据库了,并且很多公司的面试都要求面试 者有MongoDB的使用经验,至于非关系数据库与关系型数据库之间的区别大家可以自行百度.但是作为程序员的我们,既然大部 ...

  2. Codeforces243C-Colorado Potato Beetle(离散化+bfs)

    Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. Th ...

  3. poj2196

    Specialized Four-Digit Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7238   A ...

  4. 【Cocos2d-X游戏实战开发】捕鱼达人之游戏场景的创建(六)

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为cocos2d-x-2.1.5)    博主发现前两个系列的学习教程被严重抄袭,在这里呼吁大家请尊重开发者的劳动成果, 转载的时候请务必注 ...

  5. Android使用bindService启动服务

    1.Service package com.example.ebobo; import java.util.Timer; import java.util.TimerTask; import andr ...

  6. thinkphp实现excel数据的导入导出

    下载地址:phpexcel.rar 实现步骤: 一:在http://phpexcel.codeplex.com/下载最新PHPExcel放到Vendor下,注意位置:ThinkPHP\Extend\V ...

  7. POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

    解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...

  8. Android控件TextView的实现原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...

  9. 移动开发(webapp)过程中的小细节总结

    1.阻止旋转屏幕时自动调整字体大小 html, body, form, fieldset, p, div, h1, h2, h3, h4, h5, h6 { -webkit-text-size-adj ...

  10. RSA加密算法及其与SpringMVC集成

    如有不足,敬请各位提出批评,定会改正.THX! 本文介绍的是RSA加密算法+Spring Security在SpringMVC中的集成使用. Spring Security是什么? 引用: Sprin ...