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")

 

上一篇:leetcode 165. Compare Version Numbers


下一篇:165.比较版本号