python3刷leetcode第165题165.compare version number

2023-11-05

class Solution:
    def compareVersion(self,version1:str,version2:str) -> int:
        version1 = version1.split('.')
        version2 = version2.split('.')
        
        n = max(len(version1),len(version2))
        print("这是数字1",version1)
        print("这是数字2",version2)
        i = 0 #这个是索引
        print("最大长度:",n)
        
        
        for i in range(n):
            v1 = int(version1[i]) if i < len(version1) else None
            v2 = int(version2[i]) if i < len(version2) else None
            print("这是第几次循环:",i)
            print(v1)
            print(v2)
            if not v2 and v1:
                return 1
            elif not v1 and v2:
                return -1
            elif v1 and v2:
                if v1 > v2:
                    return 1
                elif v1 < v2:
                    return -1
        return 0
                
c = Solution()
print("这是最终结果",c.compareVersion("1.01","1.0.1"))
 #这里要注意一下,用“”输入字符串,直接输入的是整数,会报错。

这是leetcode python的第165题,code来自于,未取得授权,仅仅是个人日记用。
https://www.youtube.com/watch?v=GNSTPuSBZFk
Michelle小梦想家

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python3刷leetcode第165题165.compare version number 的相关文章

随机推荐