-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path165-CompareVersionNumbers.py
More file actions
30 lines (30 loc) · 1.11 KB
/
165-CompareVersionNumbers.py
File metadata and controls
30 lines (30 loc) · 1.11 KB
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
30
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
v1 = version1.split('.')
v2 = version2.split('.')
if(len(v1) >= len(v2)):
for i, val in enumerate(v1):
compareValue = v2[i] if(i < len(v2)) else 0
# print val
# print compareValue
# 去除前导0 通过转str再转回int的方式
val = int(str(val))
compareValue = int(str(compareValue))
if(val > compareValue):
return 1
if(val < compareValue):
return -1
pass
return 0
else:
for i, val in enumerate(v2):
compareValue = v1[i] if(i < len(v1)) else 0
# 去除前导0 通过转str再转回int的方式
val = int(str(val))
compareValue = int(str(compareValue))
if(val > compareValue):
return -1
if(val < compareValue):
return 1
pass
return 0