根据题意,按"."分割两个版本,比较每个字符的大小即可,故先将字符串分割成数组,然后按索引对比,超出部分查看是否为0
代码:

class Solution:
    def compare(self , version1 , version2 ):
        # write code here
        vs_1, vs_2 = version1.split("."), version2.split(".")
        v_1, v_2 = 0, 0
        while v_1 < len(vs_1) or v_2 < len(vs_2):
            if v_1 < len(vs_1) and v_2 < len(vs_2):
                if int(vs_1[v_1]) > int(vs_2[v_2]):
                    return 1
                elif int(vs_1[v_1]) < int(vs_2[v_2]):
                    return -1
            elif v_1 < len(vs_1):
                if int(vs_1[v_1]) > 0:
                    return 1
            elif v_2 < len(vs_2):
                if int(vs_2[v_2]) > 0:
                    return -1
            v_1 += 1
            v_2 += 1
        return 0