LeetCode 笔记21 生成第k个排列

题目是这样的:

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.

就是说,按字典序给出第k个排列。

因为之前做了“生成下一个排列”的题目,就直接拿过来用了。不料超时,于是只能用数学的方法做了。当然这也不是我想出来的,而是搜出来的(逃。。。)

基本的想法是,对于第k个排列,{a1, a2, a3, ..., an}, a1 是多少呢?

因为{a2, a3, ..., an} 一共有 (n-1)!种,a1在num中的index相当于 k / (n-1)!。换句话解释,就是一共有n个block,每个block大小是(n-1)!这么大,现在要求的就是在哪个block。

同理,求a2的时候,a1(在哪个block)已经求出来了,update k = k % (n-1)!, block的大小变成了(n-2)!, 这又是一个子问题了。

代码如下:

 1 public String getPermutation(int n, int k) {
 2         int[] num = new int[n];
 3         int perNumCount = 1;
 4
 5         for(int i = 0; i < n; i++) {
 6             num[i] = i+1;
 7             perNumCount *= i + 1;
 8         }
 9         k--;
10         StringBuilder sb = new StringBuilder();
11         for(int i = 0; i < n; i++) {
12             perNumCount = perNumCount / (n - i);
13             int choosed = k / perNumCount;
14             sb.append(String.valueOf(num[choosed]));
15             for(int j = choosed; j < n - i - 1; j++) {
16                 num[j] = num[j+1];
17             }
18             k = k % perNumCount;
19         }
20         return sb.toString();
21     }
时间: 2024-08-05 17:58:11

LeetCode 笔记21 生成第k个排列的相关文章

leetcode排列,求第k个排列

stl 中的下一个排列在写一遍忘了 写个1个多小时,使用递归写的,错误就在我使用一个list保存当前剩下的数,然后利用k/(n-1)!的阶乘就是删除的数字,但进过观察, 比如 list={1,2,3} 分成3组: 1  {2,3} 2 {1,3} 3 {1,2} 确定位于哪个组,然后确定位于哪个组的第几个nyoj 511. 求第3个排列   ,3%2=1,删除 list就是第3个数3,其实呢是第2个树2 ,所以   计算方法为 (k-1)/(n-1)! 另外一个对于下一组,k%(n-1)!也不行

[leetcode] 60. 第k个排列

60. 第k个排列 还是使用之前用过多次的nextPermutation方法...(几乎所有跟排列相关的题都是同一个题- -) class Solution { public String getPermutation(int n, int k) { int[] nums = new int[n]; for (int i = 1; i <= n; i++) { nums[i - 1] = i; } while (k > 1) { nextPermutation(nums); k--; } St

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

第k个排列

给定 n 和 k,求123..n组成的排列中的第 k 个排列. 注意事项 1 ≤ n ≤ 9 样例 对于 n = 3, 所有的排列如下: 123 132 213 231 312 321 如果 k = 4, 第4个排列为,231. 1 public class PaiLieK 2 { 3 public String getPermutation(int n, int k) 4 { 5 List<Integer> list = new ArrayList<>(); 6 List<

Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation)

Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation) 作为表单,字段验证当然是不能少的,今天我们来一起看看Ext.Net FormPanel的字段验证功能. 约束功能 为了防止用户输入不合法的数据,我们可以使用约束功能来限制用户的输入,例如TextField的 MinLength/MaxLength,NumberField的MinValue/MaxValue,DateField的MinDate /MaxDate等属性,它们可以将用户输入的值限定在一个合

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

[LeetCode系列]括号生成问题

给定n, 返回所有匹配的n对括号的可能形式. 如 给定 n = 3, 一个解集是: "((()))", "(()())", "(())()", "()(())", "()()()" 本题解法的思路是 使用栈seq保存经历的字符串状态; 使用栈valid保存对应的字符串中有效的括号对个数; 当seq不为空时(即回溯未结束): 当前的字符串和其中有效的括号对个数分别出栈; 1. 如果字符串长度等于待求解的长度,

ligh1060(求字符串第k大排列)组合数学

题意:求给定字符串(有重复字符)第k大排列. 解法:先判断字符串的所有排列是否够k个.然后从左向右每一位每一位确定.简单的组合数学. 代码: /**************************************************** * author:xiefubao *******************************************************/ #pragma comment(linker, "/STACK:102400000,10240000

[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 permutations in order, we get the following sequence for n = 3: "123" "132" "213" "231" "312" "321&