题解 | 比较版本号
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 比较版本号
# @param version1 string字符串
# @param version2 string字符串
# @return int整型
#
class Solution:
def compare(self , version1: str, version2: str) -> int:
# 点分割,整数化
version1_l = version1.split('.')
version2_l = version2.split('.')
version1_l = [int(i) for i in version1_l]
version2_l = [int(i) for i in version2_l]
# 补齐0
if len(version1_l)!=len(version2_l):
max_len = max(len(version1_l),len(version2_l))
min_l = version1_l if len(version1_l)<len(version2_l) else version2_l
for i in range(max_len-len(min_l)):
min_l.append(0)
# 比较
version1_l = tuple(version1_l)
version2_l = tuple(version2_l)
if version1_l<version2_l:
return -1
elif version1_l>version2_l:
return 1
else:
return 0
