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

[Swift]LeetCode670. 最大交换 | Maximum Swap

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

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

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

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

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.

Example 1:

Input: 2736
Output: 7236
Explanation: Swap the number 2 and the number 7. 

Example 2:

Input: 9973
Output: 9973
Explanation: No swap. 

Note:

  1. The given number is in the range [0, 10^8]

给定一个非负整数,你至多可以交换一次数字中的任意两位。返回你能得到的最大值。

示例 1 :

输入: 2736
输出: 7236
解释: 交换数字2和数字7。

示例 2 :

输入: 9973
输出: 9973
解释: 不需要交换。

注意:

  1. 给定数字的范围是 [0, 10^8]

Runtime: 8 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func maximumSwap(_ num: Int) -> Int {
 3         var arr:[Character] = Array(String(num))
 4         var res:Int = num
 5         var n:Int = arr.count
 6         var str:String = String()
 7         for i in 0..<n
 8         {
 9             for j in (i + 1)..<n
10             {
11                 arr.swapAt(i,j)
12                 str = String(arr)
13                 res = max(res, Int(str) ?? 0)
14                 arr.swapAt(i,j)
15             }
16         }
17         return res
18     }
19 }

8ms

 1 class Solution {
 2     func maximumSwap(_ num: Int) -> Int {
 3         let str = String(num)
 4         var arr = Array(str)
 5         var sortedNumbers = arr.sorted()
 6         sortedNumbers = sortedNumbers.reversed()
 7         
 8         var index = 0
 9         while index < arr.count - 1 && arr[index] == sortedNumbers[index]{            
10             index += 1
11         }
12         if index >= arr.count - 1 {
13             return num
14         }
15         let temp = arr[index]
16         var indexNeedChanged = index
17         for i in (index ..< arr.count).reversed() {
18             if arr[i] == sortedNumbers[index] {
19                 indexNeedChanged = i
20                 break
21             }
22         }
23         arr[index] = sortedNumbers[index]    
24         arr[indexNeedChanged] = temp
25         let ns = String(arr)
26         return Int(ns)!
27     }
28 }

20ms

 1 class Solution {
 2     func maximumSwap(_ num: Int) -> Int {
 3         var nums = [String]()
 4         for char in "\(num)" {
 5             nums.append(char.description)
 6         }
 7         
 8         let count = nums.count
 9         for i in 0..<count - 1 {
10             var mark = count - 1
11             for j in (i + 1..<count).reversed() {
12                 if nums[j] > nums[mark] {
13                     mark = j
14                 }
15             }
16             print(i)
17             print(mark)
18             if nums[mark] > nums[i] {
19                 let temp = nums[i]
20                 nums[i] = nums[mark]
21                 nums[mark] = temp
22                 return Int(nums.joined()) ?? 0
23             }
24         }
25         return num
26     }
27 }

24ms

 1 class Solution {
 2     func maximumSwap(_ num: Int) -> Int {
 3         var num = num
 4         var nums: [Int] = []
 5         
 6         while num > 0 {
 7             nums.append(num % 10)
 8             num /= 10
 9         }
10         
11         for n in stride(from: nums.count-1, to: -1, by: -1) {
12             var mx = nums[0]
13             var idx = 0
14             for k in 0..<n {
15                 if nums[k] > mx {
16                     mx = nums[k]
17                     idx = k
18                 }
19             }
20             if mx > nums[n] {
21                 nums.swapAt(idx, n)
22                 break
23             }
24         }
25         
26         var res = 0
27         for n in nums.reversed() {
28             res = res * 10 + n
29         }
30         
31         return res
32     }
33 }

 

posted @ 2019-03-07 21:50  为敢技术  阅读(299)  评论(0编辑  收藏  举报