leetcode 165. Compare Version Numbers

链接地址:leetcode链接

思路 :通过’.’把字符串分割成数组,再进行比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution(object):
def compareVersion(self, version1, version2):
"""
:type version1: str
:type version2: str
:rtype: int
"""
v1 = [int(i) for i in version1.split('.')]
v2 = [int(i) for i in version2.split('.')]
if len(v1)>len(v2):
for i in range(len(v2)):
if v1[i] > v2[i]:
return 1
elif v1[i] < v2[i]:
return -1
if max(v1[len(v2):]) > 0:return 1
else:return 0
elif len(v1) < len(v2):
for i in range(len(v1)):
if v1[i] > v2[i]:
return 1
elif v1[i] < v2[i]:
return -1
if max(v2[len(v1):]) > 0:return -1
else:return 0
else:
for i in range(len(v1)):
if v1[i] > v2[i]:
return 1
elif v1[i] < v2[i]:
return -1
return 0