every blog every motto: You will never know unless you try
0. 前言
冲冲冲
1. 字符串
1.1 题目
1.2 代码
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
ver1_list = version1.split('.')
ver2_list = version2.split('.')
if len(ver1_list) > len(ver2_list):
ver2_list += (len(ver1_list) - len(ver2_list)) * ['0']
else:
ver1_list += (len(ver2_list) - len(ver1_list)) * ['0']
for index in range(len(ver1_list)):
temp1 = ''
temp2 = ''
c_index1 = 0
c_index2 = 0
for i in ver1_list[index]:
if i != '0' and c_index1 < len(ver1_list[index]) - 1:
temp1 = ver1_list[index][c_index1:]
break
elif c_index1 == len(ver1_list[index]) - 1:
temp1 = ver1_list[index][-1]
else:
c_index1 += 1
for i in ver2_list[index]:
if i != '0' and c_index2 < len(ver2_list[index]) - 1:
temp2 = ver2_list[index][c_index2:]
break
elif c_index2 == len(ver2_list[index]) - 1:
temp2 = ver2_list[index][-1]
else:
c_index2 += 1
temp1_num = int(temp1)
temp2_num = int(temp2)
if temp1_num > temp2_num:
return 1
elif temp1_num < temp2_num:
return -1
else:
if index != len(ver1_list) - 1:
continue
else:
return 0
1.3 结果
|