Python solution with detailed explanation

  • 0


    Compare Version Numbers

    • The only thing to consider is that a version may have zero or more ".". We can have "".
    class Solution(object):
        def get_version(self, v):
            if "." in v:
                output = v.split(".")
                return [int(x) for x in output]
                return [int(v)]
        def compareVersion(self, version1, version2):
            :type version1: str
            :type version2: str
            :rtype: int
            v1 = self.get_version(version1)
            v2 = self.get_version(version2)
            N = max(len(v1), len(v2))
            for i in range(N):
                m_1 = v1[i] if i < len(v1) else 0
                m_2 = v2[i] if i < len(v2) else 0
                if m_1 > m_2:
                    return 1
                elif m_1 < m_2:
                    return -1
            return 0

Log in to reply

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