python leetcode 165. Compare Version Numbers

class Solution(object):
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        v1=version1.split('.')
        v2=version2.split('.')
        j=len(v1)-1
        while j>=0 and int(v1[j])==0:
            j-=1
        v1=v1[:j+1]
        
        j=len(v2)-1
        while j>=0 and int(v2[j])==0:
            j-=1
        v2=v2[:j+1]
        i=0
        while i<len(v1) and i<len(v2):
            if int(v1[i])>int(v2[i]): return 1
            elif int(v1[i])<int(v2[i]): return -1
            else: i+=1
        if i<len(v1): return 1
        if i<len(v2): return -1
        return 0

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/85013004