Simple python code, easy understand


  • 0
    H
    class Solution(object):
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        v1 = version1.split('.')
        v2 = version2.split('.')
        
        i, M = 0, max(len(v1), len(v2))
        
        for i in range(M):
            n1 = int(v1[i]) if i < len(v1) else 0
            n2 = int(v2[i]) if i < len(v2) else 0
            
            if n1 != n2:
                return 1 if n1 > n2 else -1
        
        return 0

Log in to reply
 

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