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

[Swift]LeetCode1053.交换一次的先前排列 | Previous Permutation With One Swap

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

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

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

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

Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.

Example 1:

Input: [3,2,1]
Output: [3,1,2]
Explanation: 
Swapping 2 and 1.

Example 2:

Input: [1,1,5]
Output: [1,1,5]
Explanation: 
This is already the smallest permutation.

Example 3:

Input: [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: 
Swapping 9 and 7.

Example 4:

Input: [3,1,1,3]
Output: [1,1,3,3]

Note:

  1. 1 <= A.length <= 10000
  2. 1 <= A[i] <= 10000

 给你一个正整数的数组 A(其中的元素不一定完全不同),请你返回可在 一次交换(交换两数字 A[i] 和 A[j] 的位置)后得到的、按字典序排列小于 A 的最大可能排列。

如果无法这么操作,就请返回原数组。

示例 1:

输入:[3,2,1]
输出:[3,1,2]
解释:
交换 2 和 1

示例 2:

输入:[1,1,5]
输出:[1,1,5]
解释: 
这已经是最小排列

示例 3:

输入:[1,9,4,6,7]
输出:[1,7,4,6,9]
解释:
交换 9 和 7

示例 4:

输入:[3,1,1,3]
输出:[1,1,3,3]

提示:

  1. 1 <= A.length <= 10000
  2. 1 <= A[i] <= 10000

Runtime: 264 ms
Memory Usage: 21.7 MB
 1 class Solution {
 2     func prevPermOpt1(_ A: [Int]) -> [Int] {
 3         var A = A
 4         let n:Int = A.count
 5         for i in stride(from:n - 1,to:0,by:-1)
 6         {
 7             if A[i-1] <= A[i] {continue}
 8             let id:Int = i - 1
 9             for j in stride(from:n - 1,to:id,by:-1)
10             {
11                 if A[id] <= A[j] {continue}
12                 A.swapAt(id,j)
13                 return A
14             }
15         }
16         return A
17     }
18 }

268ms

 

 1 class Solution {
 2     func prevPermOpt1(_ A: [Int]) -> [Int] {
 3         var A = A, i = A.count - 2
 4         while i >= 0 && A[i] <= A[i+1] {
 5             i -= 1
 6         }
 7         
 8         guard i >= 0 else { return A }
 9         
10         var lo = i + 1, hi = A.count
11         while lo < hi {
12             let mid = lo + (hi - lo) / 2
13             if A[i] > A[mid] {
14                 lo = mid + 1
15             } else {
16                 hi = mid
17             }
18         }
19         
20         var target = lo - 1
21         while target > i && A[target] == A[target-1] {
22             target -= 1
23         }
24         A.swapAt(i, target)
25         return A
26     }
27 }

276ms

 1 class Solution {
 2     func prevPermOpt1(_ A: [Int]) -> [Int] {
 3         var A = A, i = A.count - 2
 4         while i >= 0 && A[i] <= A[i+1] {
 5             i -= 1
 6         }
 7         
 8         guard i >= 0 else { return A }
 9         
10         var lo = i + 1, hi = A.count
11         while lo < hi {
12             let mid = lo + (hi - lo) / 2
13             if A[i] > A[mid] {
14                 lo = mid + 1
15             } else {
16                 hi = mid
17             }
18         }
19         
20         A.swapAt(i, lo - 1)
21         return A
22     }
23 }

 

posted @ 2019-05-26 09:18  为敢技术  阅读(507)  评论(0编辑  收藏  举报