w********n 发帖数: 4752 | 1 It's my codes below. Any other better solutions?
class Solution:
# @param version1, a string
# @param version2, a string
# @return an integer
def compareVersion(self, version1, version2):
v1=version1.split('.')
v2=version2.split('.')
size1=len(v1)
size2=len(v2)
size=max(size1,size2)
res1=0
for i in range(size1):
res1 += 10**(size-i)*int(v1[i])
res2=0
for i in range(size2):
res2 += 10**(size-i)*int(v2[i])
if res1>res2:
return 1
elif res1
return -1
else:
return 0
| t********5 发帖数: 522 | 2 class Solution:
# @param version1, a string
# @param version2, a string
# @return an integer
def compareVersion(self, version1, version2):
v1 = version1.split('.')
v2 = version2.split('.')
while v1 and v2:
sectionV1 = int(v1.pop(0))
sectionV2 = int(v2.pop(0))
if sectionV1 > sectionV2:
return 1
elif sectionV1 < sectionV2:
return -1
if v1 and int(''.join(v1)):
return 1
elif v2 and int(''.join(v2)):
return -1
return 0 | w********n 发帖数: 4752 | 3 thanks!!
【在 t********5 的大作中提到】 : class Solution: : # @param version1, a string : # @param version2, a string : # @return an integer : def compareVersion(self, version1, version2): : v1 = version1.split('.') : v2 = version2.split('.') : : while v1 and v2: : sectionV1 = int(v1.pop(0))
|
|