5 line python solution


  • 1
    A
    def compareVersion(version1, version2):
        f1=map(lambda x: int(x) ,version1.split('.'))
        f2=map(lambda x: int(x) ,version2.split('.'))
        f1+=([0]*(len(f2)-len(f1)))
        f2+=([0]*(len(f1)-len(f2)))
        return 1 if a>b else (-1 if a<b else 0)

  • 0
    M

    A little improvement in comprehension

    def compareVersion(self, version1, version2):
        v1 = map(int, version1.split('.'))
        v2 = map(int, version2.split('.'))
        v1 += [0] * (len(v2)-len(v1))
        v2 += [0] * (len(v1)-len(v2))
        if v1 > v2:
            return 1
        if v1 < v2:
            return -1
        return 0

  • 0
    W
    def compareVersion(self, version1, version2):
        # instead of comparing the string translate it float and add it up 
        master_v_1=0
        master_v_2=0
        if '.' in version1:
            v_1_list=version1.split('.')
            digit_index=1.0
            for v_1 in v_1_list:
                master_v_1=master_v_1+float(v_1)*digit_index
                digit_index=digit_index/10
        else: 
            master_v_1=master_v_1+float(version1)
    
        if '.' in version2:
            v_2_list=version2.split('.')
            digit_index=1.0
            for v_2 in v_2_list:
                master_v_2=master_v_2+float(v_2)*digit_index
                digit_index=digit_index/10
        else: 
            master_v_2=master_v_2+float(version2)
        if master_v_2>master_v_1:
            return -1
        elif master_v_2<master_v_1:
            return 1
        else: 
            return 0
    

    I think if we represent the version number as an float, it is much easier to understand .


  • 0
    D

    What is a and b here?


  • 0
    D

    a little improvement.

    def compareVersion(self, version1, version2):
        v1 = map(int, version1.split('.'))
        v2 = map(int, version2.split('.'))
        v1 += [0] * (len(v2)-len(v1))
        v2 += [0] * (len(v1)-len(v2))
        return cmp(v1, v2)
    

Log in to reply
 

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