题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 比较版本号 # @param version1 string字符串 # @param version2 string字符串 # @return int整型 # class Solution: def compare(self , version1: str, version2: str) -> int: v1 = version1.split(".") v2 = version2.split(".") v1n = [int(x) for x in v1 if int(x) > 0] v2n = [int(x) for x in v2 if int(x) > 0] minLen = min(len(v1n), len(v2n)) for i in range(0, minLen): if v1n[i] > v2n[i]: return 1 if v1n[i] < v2n[i]: return -1 if len(v1n) > len(v2n): return 1 if len(v1n) < len(v2n): return -1 return 0