其他分享
首页 > 其他分享> > 165. 比较版本号

165. 比较版本号

作者:互联网

class Solution(object):
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """

        version1 = version1.split(".")
        version2 = version2.split(".")

        if len(version1) > len(version2):
            for _ in range(len(version1) - len(version2)):
                version2.append("0")
        else:
            for _ in range(len(version2) - len(version1)):
                version1.append("0")

        for i in range(len(version1)):
            s1,s2=version1[i],version2[i]

            _=0
            for _ in range(len(s1)):
                if s1[_]!="0":
                    break

            s1=s1[_:]

            _=0
            for _ in range(len(s2)):
                if s2[_]!="0":
                    break

            s2=s2[_:]

            if len(s1)==0:
                s1=0
            else:
                s1=int(s1)

            if len(s2)==0:
                s2=0
            else:
                s2=int(s2)

            if s1>s2:
                return 1
            elif s1<s2:
                return -1

        return 0


# print Solution().compareVersion(version1="1.0", version2="1")

 

标签:version1,version2,版本号,s2,s1,len,range,165,比较
来源: https://blog.csdn.net/moshiyaofei/article/details/100066145