题解 | #比较版本号#
比较版本号
http://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 比较版本号
# @param version1 string字符串
# @param version2 string字符串
# @return int整型
#
class Solution:
def compare(self , version1: str, version2: str) -> int:
# write code here
list1 = version1.split('.')
list2 = version2.split('.')
length1 = len(list1)
length2 = len(list2)
n = length1 - length2
if n < 0:
for _ in range(n):
list1.append('0')
elif n > 0:
for _ in range(n):
list2.append('0')
i = 0
while i < len(list1):
if int(list1[i]) > int(list2[i]):
return 1
elif int(list1[i]) < int(list2[i]):
return -1
else:
i += 1
return 0