专栏首页SnailTyanLeetcode 165. Compare Version Numbers

Leetcode 165. Compare Version Numbers

1. Description

2. Solution

  • Version 1
class Solution:
    def compareVersion(self, version1: str, version2: str) -> int:
        v1 = version1.split('.')
        v2 = version2.split('.')
        
        length1 = len(v1)
        length2 = len(v2)
            
        for i in range(min(length1, length2)):
            if int(v1[i]) < int(v2[i]):
                return -1
            elif int(v1[i]) > int(v2[i]):
                return 1
        if length1 > length2:
            for i in range(length2, length1):
                if int(v1[i]) > 0:
                    return 1
        else:
            for i in range(length1, length2):
                if int(v2[i]) > 0:
                    return -1
        return 0

Reference

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Leetcode 165 Compare Version Numbers

    Compare two version numbers version1 and version2. If version1 > version2 retu...

    triplebee
  • leetcode 165 Compare Version Numbers

    Compare two version numbers version1 and version2. If version1 > version2 ret...

    流川疯
  • LeetCode 165. Compare Version Numbers

    ShenduCC
  • Leetcode: Compare Version Numbers

    题目: Compare two version numbers version1 and version2. If version1 > version...

    卡尔曼和玻尔兹曼谁曼
  • Leetcode Golang 165. Compare Version Numbers.go

    版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/88992819

    anakinsun
  • 算法细节系列(34):再见字符串(2)

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.n...

    用户1147447
  • DP、DFS-LeetCode 198、332、165(DP, DFS)

    你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小...

    算法工程师之路
  • numpy.isclose

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    狼啸风云
  • numpy.allclose

    numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)[source]

    狼啸风云

扫码关注云+社区

领取腾讯云代金券