Leetcode OJ : Compare Version Numbers Python solution
Total Accepted: 12400 Total Submissions: 83230
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.
Solution:
class Solution:
# @param version1, a string
# @param version2, a string
# @return an integer
def compareVersion(self, version1, version2):
splited1, splited2 = version1.split('.'), version2.split('.')
diff = len(splited1) - len(splited2) ext = splited1 if diff < 0 else splited2;
ext.extend(['' for i in range(abs(diff))]) for a, b in zip(splited1, splited2):
ret = cmp(int(a), int(b))
if ret != 0:
return ret
return 0
Leetcode OJ : Compare Version Numbers Python solution的更多相关文章
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- leetcode:Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Leetcode 165 Compare Version Numbers
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- leetcode4 Valid Palindrome回文数
Valid Palindrome回文数 whowhoha@outlook.com Question: Given a string, determine if it is a palindrome, ...
- 如何设置Samza的metrics
参考这个里边对API的调用 http://samza.incubator.apache.org/learn/documentation/0.7.0/container/metrics.html 参考这 ...
- POJ-1088 滑雪 (包含部分自用测试数据)
这题最简单的想法是深搜+记录,由于数据量比较小.这么做可以AC.如果在h大的情况下这种递归方法总会有一些问题. 如果转换一下,这个可以使用递推来解决,先对高度进行由低到高的排序,然后顺序对这些高度计算 ...
- Razor视图引擎布局
不需要像过去aspx一样,使用.Master文件,而是统一使用.cshtml 或 .vbhtml文件.但文件名一般以 _开头,这样做文件不会当做View显示出来 使用@RenderBody() 表示替 ...
- NAT负载均衡
NAT(Network Address Translation 网络地址转换)简单地说就是将一个IP地址转换为另一个IP地址,一般用于未经注册的内部地址与合法的.已获注册的Internet IP地址间 ...
- python脚本工具 - 3 目录遍历
遍历系统中某一目录下的所有文件名 #! /usr/bin/python # coding:utf-8 import os def dirList(path): filelist = os.listdi ...
- java遍历树(深度遍历和广度遍历
java遍历树如现有以下一颗树:A B B1 B11 B2 B22 C C ...
- 利用循环removeChild删除节点只删除一半问题
<!DOCTYPE html> <html> <head> <title>adduser.html</title> ...
- 函数重载二义性:error C2668: 'pow' : ambiguous call to overloaded function
2013-07-08 14:42:45 当使用的函数时重载函数时,若编译器不能判断出是哪个函数,就会出现二义性,并给出报错信息. 问题描述: 在.cpp代码中用到pow函数,如下: long int ...
- Android Framework------之Input子系统
下面这是基于Android4.2代码的关于Input子系统的笔记.在这篇笔记中,只涉及Android相关的东西,关于Linux内核中对各种输入设备的统一,在本文中不作说明.此外,由于才疏学浅,文中难免 ...