Step 1, split version string by '.' to get integer on each level
Step 2, recursively compare integer on each level. By default, integer on each level = 0
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
v1 = list(map(int, version1.split('.')))
v2 = list(map(int, version2.split('.')))
def compare(level):
left = v1[level] if level < len(v1) else 0
right = v2[level] if level < len(v2) else 0
if left < right:
return -1
if left > right:
return 1
return 0 if level >= len(v1) and level >= len(v2) else compare(level+1)
return compare(0)
No comments:
Post a Comment