首页 > 代码库 > [leetcode]Compare Version Numbers

[leetcode]Compare Version Numbers

难度是Easy,开始没考虑那么多,直接撸。。。然后当然错了

简单的想法,把两个version split成两个num的list,然后再比较。。。

 

class Solution:    # @param a, a string    # @param b, a string    # @return a boolean     def compareVersion(self, version1, version2):        def compareInt(a, b) :            if (a > b):                return 1            elif (a < b):                return -1            else:                return 0        ver1 = version1.split(.)        ver2 = version2.split(.)        num1 = map(lambda x : int(x), ver1)        num2 = map(lambda x : int(x), ver2)        if (len(num1) < len(num2)) :            for i in xrange(0, len(num2) - len(num1)):                num1.append(0)        if (len(num1) > len(num2)) :            for i in xrange(0, len(num1) - len(num2)):                num2.append(0)        pos = filter(lambda (x, y) : x != y, zip(num1, num2))        if (len(pos) != 0) :            return compareInt(pos[0][0], pos[0][1])        return compareInt(len(num1), len(num2))

 

[leetcode]Compare Version Numbers