#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 比较版本号
# @param version1 string字符串
# @param version2 string字符串
# @return int整型
#
class Solution:
def compare(self , version1: str, version2: str) -> int:
# write code here
if not version1 and version2:
return -1
if version1 and not version2:
return 1
if version1 ==version2:
return 0
v1 =version1.split(".")
v2=version2.split(".")
for i in range(len(v1)):
if v1[i]=='0':
continue
temp=v1[i].lstrip("0") # 去除前导0
if temp:
v1[i]=temp
for i in range(len(v2)):
if v2[i]=='0' :
continue
temp=v2[i].lstrip("0")
if temp:
v2[i]=temp
print(v1,v2)
left =0
right=0
v1_len=len(v1)
v2_len=len(v2)
while left< v1_len or right<v2_len:
if left <v1_len and right<v2_len:
left_value=int(v1[left])
right_value=int(v2[right])
if left_value>right_value:
return 1
elif left_value<right_value:
return -1
else:
left+=1
right+=1
continue
elif left>=v1_len and right<v2_len and int(v2[right])>0: # "1.0" 与1.0.0” 相等
return -1
elif left<v1_len and right>=v2_len and int(v1[left])>0:
return 1
left+=1
right+=1
return 0