165. 比较版本号
为保证权益,题目请参考 165. 比较版本号(From LeetCode).
解决方案1
Python
python
# 165. 比较版本号
# https://leetcode-cn.com/problems/compare-version-numbers/
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
v1 = version1.split(".")
v2 = version2.split(".")
m = max(len(v1), len(v2))
for i in range(m):
if i < len(v1):
t1 = int(v1[i])
else:
t1 = 0
if i < len(v2):
t2 = int(v2[i])
else:
t2 = 0
if t1 > t2:
return 1
elif t1 < t2:
return -1
return 0
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29