Concise Python AC

  • 0
    class Solution(object):
        def compareVersion(self, v1, v2):
            :type version1: str
            :type version2: str
            :rtype: int
            # split and transform str's to arrays of integers
            v1,v2 = [int(p) for p in v1.split('.')],[int(p) for p in v2.split('.')]
            l1,l2,i,j = len(v1),len(v2),0,0
            while i<l1 and j<l2:
                if v1[i]>v2[j]: return 1
                if v1[i]<v2[j]: return -1
                i,j = i+1,j+1
            # if lengths are equal, versions are equal
            if i==l1 and j==l2: return 0
            # extract appendix
            res = v1[i:] if l1>l2 else v2[j:]
            # if appendix are filled with 0, then versions are equal
            if res.count(0)==len(res): return 0
            return 1 if l1>l2 else -1

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.