【python/M/leetcode】Compare Version Numbers

版权声明:小明酱私有,私自转载要捶你小胸口哦~ https://blog.csdn.net/alicelmx/article/details/83507519

题目

https://leetcode.com/problems/compare-version-numbers/

注意

就注意一下开始和末尾有0的情况

01 和 1
1.0 和 1
的区别

实现代码

class Solution(object):
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        version1,version2 = version1.split('.'),version2.split('.')
        
        while len(version1) or len(version2):
            if len(version1) == 0:
                version1 = [0]
            elif len(version2) == 0:
                version2 = [0]
            else:
                tmp1,tmp2 = int(version1[0]),int(version2[0])
                if tmp1 > tmp2:
                    return 1
                elif tmp1 < tmp2:
                    return -1
                else:
                    version1 = version1[1:]
                    version2 = version2[1:]
        return 0
                    
                    
        
        
            

猜你喜欢

转载自blog.csdn.net/alicelmx/article/details/83507519
今日推荐