My Python Solution


  • 0
    L
    class Solution(object):
        def compareVersion(self, version1, version2):
            """
            :type version1: str
            :type version2: str
            :rtype: int
            """
            v1 = version1.split('.')
            v2 = version2.split('.')
            
            index1 = 0
            index2 = 0
            while index1 < len(v1) and index2 < len(v2):
                if int(v1[index1]) > int(v2[index2]):
                    return 1
                
                if int(v1[index1]) < int(v2[index2]):
                    return -1
                    
                if int(v1[index1]) == int(v2[index2]):
                    index1 += 1
                    index2 += 1
                    
            while index1 < len(v1):
                if int(v1[index1]) != 0:
                    return 1
                index1 += 1
                
            while index2 < len(v2):
                if int(v2[index2]) != 0:
                    return -1
                index2 += 1
                
            return 0
    

Log in to reply
 

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