题解 | #比较版本号#

比较版本号

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


全部评论

相关推荐

Twilight_m...:还是不够贴近现实,中关村那块60平房子200万怎么可能拿的下来,交个首付还差不多
点赞 评论 收藏
分享
06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务