题解 | #比较版本号#
比较版本号
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 v1 = version1.split('.') v2 = version2.split('.') i=0 while i<len(v1) or i<len(v2): if i>=len(v1): t1 = 0 else: t1 = int(v1[i]) if i>=len(v2): t2 = 0 else: t2 = int(v2[i]) if t1>t2: return 1 elif t1<t2: return -1 else: i+=1 return 0