-
Notifications
You must be signed in to change notification settings - Fork 0
/
165CompareVersionNumbers.py
executable file
·41 lines (37 loc) · 1.07 KB
/
165CompareVersionNumbers.py
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
33
34
35
36
37
38
39
40
41
class Solution(object):
def compareVersion(self, version1, version2):
"""
:type version1: str
:type version2: str
:rtype: int
"""
m_vseq1 = version1.split('.')
m_vseq2 = version2.split('.')
m_vlen1 = len(m_vseq1)
m_vlen2 = len(m_vseq2)
if m_vlen1 <= m_vlen2:
m_shortlen = m_vlen1
else:
m_shortlen = m_vlen2
for pos in range(m_shortlen):
if int(m_vseq1[pos]) > int(m_vseq2[pos]):
return 1
elif int(m_vseq1[pos]) < int(m_vseq2[pos]):
return -1
else:
continue
if m_vlen1 > m_vlen2:
m_leftseq = m_vseq1
elif m_vlen1 < m_vlen2:
m_leftseq = m_vseq2
else:
return 0
for m_val in m_leftseq[m_shortlen:]:
if int(m_val) > 0:
if m_vlen1 > m_vlen2:
return 1
else:
return -1
return 0
c = Solution()
print(c.compareVersion("1.0", "1"))