★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qzyjilbd-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Compare two version numbers version1 and version2.
If version1 > version2
return 1;
if version1 < version2
return -1;
otherwise return 0
.git
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.github
Example 1:微信
Input: = "0.1", = "1.1" Output: -1version1version2
Example 2:app
Input: = "1.0.1", = "1" Output: 1version1version2
Example 3:ide
Input: = "7.5.2.4", = "7.5.3" Output: -1version1version2
比較兩個版本號 version1 和 version2。
若是 version1 > version2
返回 1
,若是 version1 < version2
返回 -1
, 除此以外返回 0
。spa
你能夠假設版本字符串非空,而且只包含數字和 .
字符。code
.
字符不表明小數點,而是用於分隔數字序列。component
例如,2.5
不是「兩個半」,也不是「差一半到三」,而是第二版中的第五個小版本。htm
示例 1:
輸入: = "0.1", = "1.1" 輸出: -1version1version2
示例 2:
輸入: = "1.0.1", = "1" 輸出: 1version1version2
示例 3:
輸入: = "7.5.2.4", = "7.5.3" 輸出: -1version1version2
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 }