- 浏览: 183597 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
题目中给定了一个整数n,它有n的阶乘个全排列,让我们找出第k个全排列。如果我们用回溯法写直到第k个,这样可以找到,但是代价很大,如果k很大时就会超时。我们可以将这些全排列分为n组,第0组是以1开头的,第1组以2开头,一直到第n-1组是以n开头的。这样每组中的全排列数量为(n-1)的阶乘个。我们用k%(n-1)! 这样就知道k属于哪一个组了,从组号就可以得到相应的值。然后让k = k / (n-1)!进行下一次运算。还有一点值得注意的是,为了让k取模后分在正确的组里,k要减1之后运算,例如n为3,k为3或4的时候应该在第二个组,如果直接运算的时候k为3的时候在第二个组,k为4的时候在第三个组。代码如下:
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.
题目中给定了一个整数n,它有n的阶乘个全排列,让我们找出第k个全排列。如果我们用回溯法写直到第k个,这样可以找到,但是代价很大,如果k很大时就会超时。我们可以将这些全排列分为n组,第0组是以1开头的,第1组以2开头,一直到第n-1组是以n开头的。这样每组中的全排列数量为(n-1)的阶乘个。我们用k%(n-1)! 这样就知道k属于哪一个组了,从组号就可以得到相应的值。然后让k = k / (n-1)!进行下一次运算。还有一点值得注意的是,为了让k取模后分在正确的组里,k要减1之后运算,例如n为3,k为3或4的时候应该在第二个组,如果直接运算的时候k为3的时候在第二个组,k为4的时候在第三个组。代码如下:
public class Solution { public String getPermutation(int n, int k) { StringBuilder sb = new StringBuilder(); List<Integer> list = new ArrayList<Integer>(); int[] factorial = new int[n]; factorial[0] = 1; for(int i = 1; i < n; i++) { factorial[i] = factorial[i - 1] * i; } for(int i = 1; i <= n; i++) { list.add(i); } k = k - 1; for(int i = 0; i < n; i++) { int index = k / factorial[n - 1 - i]; sb.append(list.get(index)); list.remove(index); k = k % factorial[n - 1 - i]; } return sb.toString(); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 674Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 843Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 705For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Permutation Sequence.java
js js_leetcode题解之60-permutation-sequence.js
236 Permutation Sequence 573 237 Generate Parentheses 575 238 Combination Sum 577 239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number ...
LeetCode的第60题,也称为"排列序列"(Permutation Sequence),要求我们给定一个整数n,生成所有可能的n个数字的排列,并按照字典序排序。例如,当n=3时,排列序列应该是"123", "132", "213", "231", "312", "321...
1. **Permutation Sequence**:这个问题涉及到排列组合和字符串操作。它要求生成一个给定整数n的所有可能排列的序列。这通常需要对数字进行位操作或使用回溯法。 2. **First Missing Positive**:这是一个关于数组...
Sequence 7 Two Sum Hash,夹逼均可 8 3Sum Hash法转换2sum 9 3Sum Closest Sort +夹逼法 10 4Sum Sort +夹逼法 11 Remove Element 12 Next Permutation 公式 13 Permutation Sequence 公式 14 Valid Sudoku 15 ...
- **2.1.13 Permutation Sequence** - 给定数字集合,返回第n个排列。 - 实现思路:利用数学方法计算出每一位上的数字。 - **2.1.14 Valid Sudoku** - 判断给定的数独是否有效。 - 实现思路:分别检查行、列和...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for ...31 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| ...
这个“leetcode_basic_60”主题可能聚焦于LeetCode上的第60题,即“Permutation Sequence”(排列序列)。这个问题涉及到组合数学、递归和字符串处理,是Python编程者常见的练习题目。 【描述】"leetcode_basic_60...
本文介绍了一种基于混沌序列构建大置换的图像加密方案。混沌理论在信息安全领域有着广泛的应用,特别是对于图像加密,因为图像数据具有大量的数据容量以及像素之间的高度相关性,传统的数据加密技术可能并不适合加密...
1. 排列流水线调度问题(Permutation Flow Shop Scheduling Problem):这是一个经典的生产调度问题,其中一系列作业需要在多个机器上按相同的顺序进行处理。目标是最小化作业完成时间,即“使跨度”最小化,或最小...
Output: A permutation (reordering) ha0 1; a0 2; : : : ; a0 ni of the input sequence such that a0 1 a0 2 a0 n. For example, given the input sequence h31; 41; 59; 26; 41; 58i, a sorting algorithm ...
提出RMSP(Random Matrix Sequence Permutation)方法,同时完成帧内宏块(MacroBlock,MB )之间、块内VLC(Variable Length Coding)码字之间双重互补的乱序加密,并利用随机序列生成随机乱序密钥矩阵序列,供每帧和每块...
- **The Reversed Gray Code Permutation**: A permutation that reverses the Gray code sequence. 3. **Sorting and Searching**: - **Basic Sorting Algorithms**: Introducing fundamental sorting ...
random_permutation = original_sequence(random_indices); end ``` 现在,`random_permutation`就是我们想要的乱序序列,它包含了原序列的所有元素,但顺序是随机的且没有元素在原来的位置。 如果你已经有一个...
for permutation in itertools.permutations(sequence, len(sequence)): print(''.join(permutation)) ``` 这段代码将输出所有由'a', 'b', 'c', 'd'组成的长度为4的排列。 2. 递归实现全排列: 递归是一种解决...
Firstly, a simple approach is put forward to calculate the makespan of job sequence. Secondly, the most position value (MPV) method is used to code the weed individuals so that fitness values can be ...