## leetcode 165 Compare Version Numbers python 原

卜霞森

``````Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37
Credits:
Special thanks to @ts for adding this problem and creating all test cases.``````

``````# coding = utf8

class Solution:
# @param {string} version1
# @param {string} version2
# @return {integer}
def compareVersion(self, version1, version2):
v1 = self.compareVersionToTuple(version1)
v2 = self.compareVersionToTuple(version2)

deep = max(len(v1), len(v2), 2)

while len(v1) < deep:
v1.append(0)

while len(v2) < deep:
v2.append(0)

for n in range(deep):
sv_1 = v1[n]
sv_2 = v2[n]
if sv_1 > sv_2:
return 1
elif sv_1 < sv_2:
return -1
return 0

def compareVersionToTuple(self, version):
v = []
substrings = version.split('.')
for str in substrings:
v.append(int(str))
return v

def __init__(self):
ans = self.compareVersion('0.1', '0.0.1')
print(ans)

solution = Solution()``````

### 卜霞森

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意： 比较版本号大小 思路： 根据逗号将版本号字符串转成数组，再比较每个数的大小 Java实现：...

yysue
2018/08/27
0
0
LeetCode：Compare Version Numbers - 比较版本号

1、题目名称 Compare Version Numbers（比较版本号） 2、题目地址 https://leetcode.com/problems/compare-version-numbers/ 3、题目内容 英文：Compare two version numbers version1 and ......

2015/09/18
376
0
Leetcode日记6

（2015/11/28） LeetCode 303 Range Sum Query - Immutable：（Easy） 1）超时的算法：每次调用sumRange函数进行一次累加运算。 2）不超时的算法：改变数组的内容，存储从0下标到当前下标所有...

fxdhdu
2015/11/28
73
0
android程序员python学习之路

1年半android经验，初次接触python,学了几天，相见恨晚，迫不及待的写了这篇记录下。python新手，请多见谅。 其实也不是最近才接触Python，大四上有一门计算机性能相关的课各种算法、工具要求...

Dynamic_2018
2018/07/08
0
0
LeetCode 279. Perfect Squares (完美平方数)

dby_freedom
2018/12/05
0
0

Httpd 整合 Tomcat 步骤

ZeroneLove

1
0
Docker笔记3——容器命令（未写完，明天整理接着写）

HappyBKs

1
0
2018个人年终总结

9
0

linux-tao

2
0
Spark共享变量

2
0