为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode60. 第k个排列 | Permutation Sequence

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9920849.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2018-11-07 09:55  为敢技术  阅读(407)  评论(0编辑  收藏  举报