class Solution(object):
def compareVersion(self, version1, version2):
"""
:type version1: str
:type version2: str
:rtype: int
"""
num_list1=version1.split('.')
num_list2=version2.split('.')
num_len1=len(num_list1)
num_len2=len(num_list2)
while num_len1 > 0:
if int(num_list1[num_len1 - 1]) == 0:
num_list1 = num_list1[:-1]
num_len1 = num_len1 -1
else:
break
while num_len2 > 0:
if int(num_list2[num_len2 - 1]) == 0:
num_list2 = num_list2[:-1]
num_len2 = num_len2 -1
else:
break
if num_len1 < num_len2:
for i in range(num_len1):
if int(num_list1[i]) < int(num_list2[i]):
return -1
elif int(num_list1[i]) > int(num_list2[i]):
return 1
return -1
else:
for i in range(num_len2):
if int(num_list1[i]) < int(num_list2[i]):
return -1
elif int(num_list1[i]) > int(num_list2[i]):
return 1
if num_len1 == num_len2:
return 0
else:
return 1
Compare Version Numbers
May 29, 2017