Permutation Sequence 解答
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):
"123"
"132"
"213"
"231"
"312"
"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 x1 and 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 解答的更多相关文章
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- 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 ...
- 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 ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 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 ...
- 【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 ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- [Leetcode] Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- 商派shopex
http://www.shopex.cn/48release/shopexsingle_exper.php 在线体验 前台体验:http://demo.shopex.com.cn/485 后台体验:h ...
- Duff策略
Tom Duff首先在C语言中提出了展开循环的构想,所以这种模式被称之为Duff策略.Duff策略背后的思想是每一次循环完成标准循环的1-8次.首先通过数组值得总数除以8来取定循环次数.Duff发现对 ...
- CentOS6.4安装mplayer
1.准备软件 mplayer官网:http://www.mplayerhq.hu/design7/news.html RPM Fusion网址:http://rpmfusion.org/ EPEL网址 ...
- 【HDU1162】Eddy's picture(MST基础题)
很基础的点坐标MST,一不留神就AC了, - - !! #include <iostream> #include <cstring> #include <cstdlib& ...
- OpenstackHigh-level-service
1,yum -y install openstack-cinder;
- Eclipse代理设置
这段时间公司实行代理上网,不仅通过浏览器上网须要不停的输入username和password,在本地调试程序时候Eclipse居然也弹出框让输入username和password. 如图: 解决的方法 ...
- C函数的实现(strcpy,atoi,atof,itoa,reverse)
在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...
- VS2012 拆分视图按钮不见,代码,设计
工具--选项--HTML设计器 然后重启就有了.
- Javascript进阶篇——( JavaScript内置对象---下)--Math对象---笔记整理
Math对象使用 Math 的属性和方法: <script type="text/javascript"> var mypi=Math.PI; var myabs=Ma ...
- CSS---------------之文本类型
通过font来改变文本,主要从以下几个方面 字体加粗,字体的风格:斜体和字体变形:小型大写字母 字体的大小 行高 字体 示例如下 p{font:italic 75%/125% "Comic ...