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

[Swift]LeetCode165. 比较版本号 | Compare Version Numbers

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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

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

Compare two version numbers version1 and version2.
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the .character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.

Example 1:

Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:

Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:

Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

比较两个版本号 version1 和 version2
如果 version1 version2 返回 1,如果 version1 version2 返回 -1, 除此之外返回 0

你可以假设版本字符串非空,并且只包含数字和 . 字符。

 . 字符不代表小数点,而是用于分隔数字序列。

例如,2.5 不是“两个半”,也不是“差一半到三”,而是第二版中的第五个小版本。

示例 1:

输入: version1 = "0.1", version2 = "1.1"
输出: -1

示例 2:

输入: version1 = "1.0.1", version2 = "1"
输出: 1

示例 3:

输入: version1 = "7.5.2.4", version2 = "7.5.3"
输出: -1

8ms
 1 class Solution {
 2     func compareVersion(_ version1: String, _ version2: String) -> Int {
 3         var numbers1 = version1.split(separator: ".").compactMap { Int(String($0)) }
 4         var numbers2 = version2.split(separator: ".").compactMap { Int(String($0)) }
 5         let numDiff = numbers1.count - numbers2.count
 6         
 7         if numDiff < 0 {
 8             numbers1.append(contentsOf: Array(repeating: 0, count: -numDiff))
 9         } else if numDiff > 0 {
10             numbers2.append(contentsOf: Array(repeating: 0, count: numDiff))
11         }
12         
13         for i in 0..<numbers1.count {
14             let diff = numbers1[i] - numbers2[i]
15             if diff != 0 {
16                 return diff < 0 ? -1 : 1
17             }
18         }
19         
20         return 0
21     }
22 }

12ms

 1 import Foundation
 2 
 3 class Solution {
 4     func compareVersion(_ version1: String, _ version2: String) -> Int {
 5         var version1Components = version1.components(separatedBy: ".")
 6         var version2Components = version2.components(separatedBy: ".")
 7         
 8         let difference = abs(version1Components.count - version2Components.count)
 9         let array = Array(repeating: "0", count: difference)
10         
11         if version1Components.count > version2Components.count {
12             version2Components.append(contentsOf: array)
13         } else if version2Components.count > version1Components.count {
14             version1Components.append(contentsOf: array)
15         }
16         
17         for (n1, n2) in zip(version1Components, version2Components) {
18             let number1 = Int(n1)!
19             let number2 = Int(n2)!
20             
21             if number1 > number2 {
22                 return 1
23             } else if number2 > number1 {
24                 return -1
25             }
26         }
27         
28         return 0
29     }
30 }

12ms

 1 class Solution {
 2     func compareVersion(_ version1: String, _ version2: String) -> Int {
 3         let lhsA = version1.components(separatedBy: ".")
 4         let rhsA = version2.components(separatedBy: ".")
 5         for i in 0 ..< max(lhsA.count, rhsA.count) {
 6             var lInt: Int, rInt: Int
 7             if i >= lhsA.count {
 8                 lInt = 0
 9             } else {
10                 lInt = Int(atoi(lhsA[i]))
11             }
12             
13             if i >= rhsA.count {
14                 rInt = 0
15             } else {
16                 rInt = Int(atoi(rhsA[i]))
17             }
18             
19             if lInt > rInt {
20                 return 1
21             } else if lInt < rInt {
22                 return -1
23             }
24         }
25         return 0
26     }
27 }

16ms

 1 class Solution {
 2     func compareVersion(_ version1: String, _ version2: String) -> Int {
 3         let arr1 = version1.split(separator: ".")
 4         let arr2 = version2.split(separator: ".")
 5         var i = 0
 6         while i < arr1.count || i < arr2.count {
 7             if i < arr1.count && i < arr2.count {
 8                 if Int(String(arr1[i]))! > Int(String(arr2[i]))! {
 9                     return 1
10                 } else if Int(String(arr1[i]))! < Int(String(arr2[i]))! {
11                     return -1
12                 }
13             } else if i < arr1.count {
14                 if Int(String(arr1[i]))! != 0 {
15                     return 1
16                 }
17             } else if i < arr2.count {
18                 if Int(String(arr2[i]))! != 0 {
19                     return -1
20                 }
21             }
22             i += 1
23         }
24         return 0
25     }
26 }

16ms

 1 class Solution {
 2     func compareVersion(_ version1: String, _ version2: String) -> Int {
 3         let ver1 = version1.components(separatedBy:".")
 4     let ver2 = version2.components(separatedBy:".")
 5     let maxLength = max(ver1.count, ver2.count)
 6     for index in 0..<maxLength {
 7         let v1:Int = index < ver1.count ? (Int)(ver1[index])! : 0
 8         let v2:Int = index < ver2.count ? (Int)(ver2[index])! : 0
 9         if (v1 > v2) {
10             return 1
11         } else if (v1 < v2) {
12             return -1;
13         }
14     }
15     return 0
16     }
17 }

24ms

 1 class Solution {
 2     func compareVersion(_ version1: String, _ version2: String) -> Int {
 3         var v1Arr = version1.components(separatedBy: ".").map{Int($0)!}
 4         var v2Arr = version2.components(separatedBy: ".").map{Int($0)!}
 5         
 6         for i in stride(from: v1Arr.count-1, to: -1, by: -1) {
 7             if v1Arr[i] == 0 {
 8                 v1Arr.remove(at: i)
 9             }else {
10                 break
11             }
12         }
13         
14         for i in stride(from: v2Arr.count-1, to: -1, by: -1) {
15             if v2Arr[i] == 0 {
16                 v2Arr.remove(at: i)
17             }else {
18                 break
19             }
20         }
21         
22         if v1Arr.count < v2Arr.count {
23             return -1 * compareVersion(version2,version1)
24         }
25         
26         for i in 0..<v2Arr.count {
27             if v1Arr[i] > v2Arr[i] {
28                 return 1
29             }
30             if v2Arr[i] > v1Arr[i] {
31                 return -1
32             }
33         }
34         if v1Arr.count > v2Arr.count {
35             return 1
36         }else {
37             return 0
38         }
39     }
40 }

 

posted @ 2018-12-16 16:45  为敢技术  阅读(850)  评论(0编辑  收藏  举报