1. Description
2. Solution
- Version 1
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
v1 = version1.split('.')
v2 = version2.split('.')
length1 = len(v1)
length2 = len(v2)
for i in range(min(length1, length2)):
if int(v1[i]) < int(v2[i]):
return -1
elif int(v1[i]) > int(v2[i]):
return 1
if length1 > length2:
for i in range(length2, length1):
if int(v1[i]) > 0:
return 1
else:
for i in range(length1, length2):
if int(v2[i]) > 0:
return -1
return 0
Reference
- https://leetcode.com/problems/compare-version-numbers/