LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

Subscribe to see which companies asked this question

来源: https://leetcode.com/problems/next-permutation/

该题在STL库里已经被实现,但是写下这种题目,有助于对模拟,还有边界条件的认识

以下是在LeetCode里提交后的排名



 只要能理清楚1234这个简单的排列的下一个排列1243和下下个排列1324是如何得到的,就可以列出以下三个关键点:

1.找出最后一个升序的位置,如果是最后两个数字,就直接交换最后两个数字

2.如果存在升序,但是不是最后两个数字,就交换升序位置的较小数字和后面数列里恰好比该数字大的数字(及比该数字大的最小数字);再将后面的数列按照从小到大的顺序排列

3.如果不存在升序的位置,就说明是一个轮回里的最后一个排列,下一个排列就是将数列反过来。

思考后发现第1点和第2点的程序是可以放在一起写

最后可以得到如下程序:

本问题还有一种问法是60题 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 (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.

Subscribe to see which companies asked this question

来源: https://leetcode.com/problems/permutation-sequence/

这就不能用上面的模拟操作来进行求解了,因为这样会超时(我试过,在n=9时超时)

所以要用数学规律来计算已知 n, k 条件下该输出什么

这里我们考虑一个数组1,2,3,4

如果固定第一个元素为1, 则后面的排列有3!个;如果固定第一个元素为2,则后面的排列有3!;同理可以依次考虑每一位固定后,后面的排列可能有多少种,这样就可以得到最终的排列,程序如下:

来自为知笔记(Wiz)

时间: 2024-09-29 17:47:50

LeetCode31 Next Permutation and LeetCode60 Permutation Sequence的相关文章

LeetCode60: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 (ie, for n = 3): 1 "123" 2 "132" 3 "213" 4 "231" 5 "312&qu

CF 500 B. New Year Permutation 并查集

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k

[LeetCode] Find Permutation 找全排列

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a s

Codeforces554D:Kyoya and Permutation

Let's define the permutation of length n as an array p?=?[p1,?p2,?...,?pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on. Kyota Ootori has ju

排列组合(permutation)系列解题报告

本文讲解4道关于permutation的题目: 1. Permutation:输出permutation--基础递归 2. Permutation Sequence: 输出字典序排列的第k个permutation--推理3. Next Permutation:给定一个permutation中的序列,求字典序它的下一个permutation是什么--逻辑推理4. Permutation II:和第一题有细微的差别: 对于一个可能有重复元素的数组输出所有permutation--有条件dfs    

uva 11129 An antiarithmetic permutation (递归)

uva 11129 An antiarithmetic permutation A permutation of n+1 is a bijective function of the initial n+1 natural numbers: 0, 1, ... n. A permutation p is called antiarithmetic if there is no subsequence of it forming an arithmetic progression of lengt

【leetcode】Next Permutation

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde

28. 字符串的全排列之第2篇[string permutation with repeating chars]

[本文链接] http://www.cnblogs.com/hellogiser/p/string-permutation-with-repeating-chars.html [题目] 输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab和cba.例如输入字符串aba,则输出由字符a.b所能排列出来的所有字符串aab.aba.baa. [分析] 之前的博文28.字符串的排列之第1篇[String

leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde