题解 | #比较版本号#

比较版本号

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


全部评论

相关推荐

09-14 20:51
四川大学 Java
慢热的鲸鱼在学习:985加粗就行了,第二个项目来不及准备也没事,省的写了问你你还不会。你只需准备面试八股和项目场景,剩下的交给985。即使面不过也没事,面试经验是最重要的,你现在不缺时间
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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