Python 6 lines, encode and compare

  • 3
        def compareVersion(self, version1, version2):
            def encode(version):
                vals = [long(v) for v in version.split(".")]
                while vals and vals[-1] == 0: # get rid of trailing 0s in the array
                return vals
            return cmp(encode(version1), encode(version2)) # default comparison of two arrays

Log in to reply

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