Leetcode 165. Compare Version Numbers

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Compare Version Numbers

2. Solution

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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/
如果有收获,可以请我喝杯咖啡!