19.2.9 [LeetCode 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 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.
  • Given k will be between 1 and n! inclusive.

Example 1:

Input: n = 3, k = 3
Output: "213"

Example 2:

Input: n = 4, k = 9
Output: "2314"

题意

求出n个连续数的第k个permutation sequence

题解

 1 class Solution {
 2 public:
 3     string getPermutation(int n, int k) {
 4         if (n <= 1)return "1";
 5         vector<int>constn(n+1, 0);
 6         vector<char>nums(n + 1);
 7         int mult = 1;
 8         for (int i = 1; i <= n; i++) {
 9             mult *= i;
10             constn[i] = mult;
11             nums[i] = i + ‘0‘;
12         }
13         string ans = "";
14         n--;
15         int _n = n;
16         while(n>1) {
17             int idx;
18             if(k%constn[n]==0)
19                 idx = k / constn[n];
20             else
21                 idx = k / constn[n] + 1;
22             ans += nums[idx];
23             nums.erase(nums.begin() + idx);
24             k = k % constn[n];
25             n--;
26             if (k == 0)break;
27         }
28         if (k == 1) {
29             ans += nums[1];
30             ans += nums[2];
31         }
32         else if (k == 2) {
33             ans += nums[2];
34             ans += nums[1];
35         }
36         else {
37             for (int i = n+1; i >= 1; i--)
38                 ans += nums[i];
39         }
40         return ans;
41     }
42 };

需要仔细一点

原文地址:https://www.cnblogs.com/yalphait/p/10357592.html

时间: 2024-10-02 02:34:19

19.2.9 [LeetCode 60] Permutation Sequence的相关文章

Leetcode 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): Given n and k, return the kth permutation sequence. Note: Given n will be between

leetcode 60 Permutation Sequence ----- java

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" "3

leetCode 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): "123" "132" "213" "231" "312" "

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

【一天一道LeetCode】#60. Permutation Sequence.

一天一道LeetCode系列 (一)题目 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 :&quo

60. Permutation Sequence java solutions

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" "3

60. Permutation Sequence(求全排列的第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): "123" "132" "213" "231" "312" "3

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): "123" "132" "213" "231" "312" "3

【leetcode】 Permutation Sequence (middle)

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" "3