-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path165.CompareVersionNumbers.py
More file actions
34 lines (32 loc) · 1.35 KB
/
165.CompareVersionNumbers.py
File metadata and controls
34 lines (32 loc) · 1.35 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
31
32
33
34
# https://leetcode.com/problems/compare-version-numbers/
# Runtime: 28 ms, faster than 80.32% of Python3 online submissions for Compare Version Numbers.
# Memory Usage: 14.2 MB, less than 60.47% of Python3 online submissions for Compare Version Numbers.
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
v1, v2 = version1.split('.'), version2.split('.')
while v1 or v2:
rev1 = v1.pop(0) if v1 else None
rev2 = v2.pop(0) if v2 else None
if not rev1 and rev2:
if int(rev2) == 0:
continue
else:
return -1
elif rev1 and not rev2:
if int(rev1) == 0:
continue
else:
return 1
elif int(rev1) < int(rev2):
return -1
elif int(rev1) > int(rev2):
return 1
else:
continue
return 0
if __name__ == '__main__':
print(Solution().compareVersion(version1="1.01", version2="1.001"))
print(Solution().compareVersion(version1="1.0", version2="1.0.0"))
print(Solution().compareVersion(version1="0.1", version2="1.1"))
print(Solution().compareVersion(version1="1.0.1", version2="1"))
print(Solution().compareVersion(version1="7.5.2.4", version2="7.5.3"))