题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
简单的实现
class Solution:
def compare(self , version1: str, version2: str) -> int:
# write code here
s1 = version1.split('.')
s2 = version2.split('.')
if len(s1) > len(s2):
s2 = s2 + [0] * (len(s1) - len(s2))
elif len(s1) < len(s2):
s1 = s1 + [0] * (len(s2) - len(s1))
for i, j in zip(s1,s2):
if int(i) > int(j):
return 1
elif int(i) < int(j):
return -1
if not i and int(j) > 0:
return -1
elif int(i) > 0 and not j:
return 1
return 0

查看14道真题和解析