I have problem with "1" and "01", what can I do with this and where can I implement it?


  • 0
    C

    Here's the code in Python

    class Solution:
        # @param version1, a string
        # @param version2, a string
        # @return an integer
        def compareVersion(self, version1, version2):
            ver1=version1.split('.')
            ver2=version2.split('.')
            
            #setting length to run for loop
            if len(ver1)>len(ver2):
                length=len(ver2)
            else:
                length=len(ver1)
            #return either -1 or 1 depends on the result
            #need to check if they are equal
            for i in range(0, length):
                if ver1[i]>ver2[i]:
                    return 1
                elif ver1[i]<ver2[i]:
                    return -1
                else:
                    trigger=1
            if trigger==1:
                if len(ver1)>length:
                    v1=0
                    for i in range(length, len(ver1)):
                        v1=v1+int(ver1[i])
                    if v1>0:
                        return 1
                elif len(ver2)>length:
                    v2=0
                    for i in range(length, len(ver2)):
                        v2=v2+int(ver2[i])
                    if v2>0:
                        return -1
                else:
                    return 0

Log in to reply
 

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