Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 977 Bytes

165.md

File metadata and controls

47 lines (34 loc) · 977 Bytes

Compare Version Numbers

Description

link


Solution

  • See Code

Remember to strip string without start-zero


Code

O(n)

class Solution:
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        version1 = [int(i) for i in version1.split('.')]
        version2 = [int(i) for i in version2.split('.')]
        
        i = 0
        while i < len(version1) and i < len(version2):
            if version1[i] > version2[i]:
                return 1
            elif version1[i] < version2[i]:
                return -1
            else:
                i = i + 1
                
        if len(version1) > len(version2) and any(version1[i:]):
            return 1
        elif len(version1) < len(version2) and any(version2[i:]):
            return -1
        
        return 0