美文网首页
165. Compare Version Numbers [Me

165. Compare Version Numbers [Me

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-06-11 15:37 被阅读0次

    165. Compare Version Numbers

    贼简单,没什么技术含量

    class Solution(object):
        def compareVersion(self, version1, version2):
            """
            :type version1: str
            :type version2: str
            :rtype: int
            """
            version1 = version1.split('.')
            version2 = version2.split('.')
            m, n = len(version1), len(version2)
            for i in range(min(m, n)):
                if int(version1[i]) > int(version2[i]):
                    return 1
                elif int(version1[i]) < int(version2[i]):
                    return -1
            if m > n:
                for i in range(n, m):
                    if int(version1[i]) > 0:
                        return 1
            elif m < n:
                for i in range(m, n):
                    if int(version2[i]) > 0 :
                        return -1
            return 0
    

    相关文章

      网友评论

          本文标题:165. Compare Version Numbers [Me

          本文链接:https://www.haomeiwen.com/subject/mzyqfctx.html