题解 | #比较版本号#
比较版本号
https://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 ver1 = [int(x) for x in version1.split('.')] ver2 = [int(x) for x in version2.split('.')] len1 = len(ver1) len2 = len(ver2) if len1>len2: length = len1 while len2<len1: len2 += 1 ver2.append(0) else: length = len2 while len1<len2: len1 += 1 ver1.append(0) ver_num1 = 0 ver_num2 = 0 for i in range(length): ver_num1 = ver_num1*10 + ver1[i] ver_num2 = ver_num2*10 + ver2[i] if ver_num1 > ver_num2: return 1 elif ver_num1 < ver_num2: return -1 else: return 0