[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:

  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"


给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。

按大小顺序列出所有排列情况,并一一标记,当 = 3 时, 所有排列如下:

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

给定 n 和 k,返回第 k 个排列。

说明:

  • 给定 n 的范围是 [1, 9]。
  • 给定 的范围是[1,  n!]。

示例 1:

输入: n = 3, k = 3
输出: "213"

示例 2:

输入: n = 4, k = 9
输出: "2314"

8ms
 1 class Solution {
 2     func getPermutation(_ num: Int, _ kth: Int) -> String {
 3         guard num > 1 else {
 4             return "1"
 5         }
 6         var vals: [Int] = Array(repeating: 1, count: 11)
 7         var nums: [Int] = Array(repeating: 1, count: 11)
 8         var kth = kth - 1
 9         for idx in 0...10 {
10             vals[idx] = idx == 0 ? 1 : vals[idx - 1] * (idx + 1)
11             nums[idx] = idx + 1
12         }
13
14         var result = ""
15         for idx in (0...num - 2).reversed() {
16             let iidx = kth / vals[idx]
17             result.append(String(nums[iidx]))
18             nums.remove(at: iidx)
19             kth %= vals[idx]
20         }
21         result.append(String(nums[0]))
22         return result
23     }
24 }


12ms

 1 class Solution {
 2     func getPermutation(_ n: Int, _ k: Int) -> String {
 3         guard n > 0, k > 0 else { return "" }
 4         if n-1 < 1 {
 5             return "\(n)"
 6         }
 7
 8         var arr: [Int] = []
 9         for i in 1...n {
10             arr.append(i)
11         }
12
13         var factList: [Int] = []
14         var current = 1
15         for i in 1...n {
16             current = current * i
17             factList.append(current)
18         }
19
20         var output = ""
21         for i in 1...n-1 {
22             var idx = Int((Double((k % factList[n-i])*arr.count)/Double(factList[n-i])).rounded(.up))-1
23             if idx < 0 {
24                 idx += arr.count
25             }
26             output.append("\(arr.remove(at: idx))")
27         }
28         output.append("\(arr.removeLast())")
29         return output
30     }
31 }


12ms

 1 class Solution {
 2     func getPermutation(_ n: Int, _ k: Int) -> String {
 3         var k = k
 4         var j: Int
 5         var f: Int = 1
 6         var s = [Character](repeating: "0", count: n)
 7         let map: [Int: Character] = [1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9"]
 8         for i in 1...n {
 9             f *= i
10             s[i-1] = map[i]!
11         }
12         k -= 1
13         for i in 0..<n {
14             f /= n - i
15             j = i + k / f
16             let c = s[j]
17             if j > i {
18                 for m in (i+1...j).reversed() {
19                     s[m] = s[m-1]
20                 }
21             }
22             k %= f
23             s[i] = c
24         }
25         return String(s)
26     }
27 }


16ms

 1 class Solution {
 2     func getPermutation(_ n: Int, _ k: Int) -> String {
 3         var nums: [Int] = []
 4         for i in 1...n {
 5             nums.append(i)
 6         }
 7         var result: String = ""
 8         backtracking(&nums, n, k-1, factorial(n - 1), &result)
 9         return result
10     }
11     func backtracking(_ nums: inout [Int], _ n: Int, _ k: Int, _ count: Int, _ result: inout String) {
12         if nums.count == 1 {
13             result += String(nums[0])
14             return
15         }
16
17         var index = k / count
18         var nextIndex = k % count
19
20
21         result += String(nums.remove(at: index))
22         backtracking(&nums, n - 1, nextIndex, count / (n - 1), &result)
23     }
24     func factorial(_ n: Int) -> Int {
25         var n = n
26         var result = 1
27         while n > 1 {
28             result *= n
29             n -= 1
30         }
31         return result
32     }
33 }


16ms

 1 class Solution {
 2     func getPermutation(_ n: Int, _ k: Int) -> String {
 3         if n == 1 { return "1" }
 4         var array = [Int]()
 5         var dp = [Int](repeating: 1, count: n + 1)
 6         for i in 1 ... n {
 7             array.append(i)
 8             dp[i] = dp[i - 1] * i
 9         }
10
11         var result = [Int]()
12         var k = k
13         while result.count < n {
14             var i = 0
15             while k > (i + 1) * dp[array.count - 1] {
16                 i += 1
17             }
18             k = k - i * dp[array.count - 1]
19             let temp = array.remove(at: i)
20             result.append(temp)
21
22         }
23         var s = ""
24         for i in 0 ..< result.count {
25             s += "\(result[i])"
26         }
27         return s
28     }
29 }

原文地址:https://www.cnblogs.com/strengthen/p/9920849.html

时间: 2024-10-21 23:42:04

[Swift]LeetCode60. 第k个排列 | Permutation Sequence的相关文章

LEETCODE60——第K个排列

1 class Solution { 2 public: 3 string getPermutation(int n, int k) { 4 string ans(n, '0'); 5 vector<bool> flag(n, false); 6 int count = 1; 7 for (int i = 1; i < n; i++) 8 count *= i; 9 int m; 10 int m_f; 11 int index = 0; 12 k = k - 1; 13 while (

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

[C++]LeetCode: 114 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" &q

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

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 replaceme

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(排列序列)

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&q

每日算法之四十二: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" "

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