题目描述:

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

解题思路:

很多细节,代码比较冗余

class Solution:
# @param version1, a string
# @param version2, a string
# @return an integer
def compareVersion(self, version1, version2):
v1 = version1.split('.')
v2 = version2.split('.')
l1 = len(v1)
l2 = len(v2)
if l1 < l2:
for i in range(l1):
if int(v1[i]) < int(v2[i]):
return -1
elif int(v1[i]) > int(v2[i]):
return 1
for i in range(l1,l2):
if int(v2[i]) > 0:
return -1
return 0 else:
for i in range(l2):
if int(v1[i]) < int(v2[i]):
return -1
elif int(v1[i]) > int(v2[i]):
return 1
for i in range(l2,l1):
if int(v1[i]) > 0:
return 1
return 0

【leetcode】Compare Version Numbers的更多相关文章

  1. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  2. 【Leetcode】【Easy】Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  3. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  4. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. [LeetCode] 165. Compare Version Numbers 比较版本数

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  6. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  7. Java for LeetCode 165 Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  8. leetcode:Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  9. Java [Leetcode 165]Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...

随机推荐

  1. dietpi请暂时不要升级为jessie

    关于升级到Debian最新稳定版jessie,作者是这样说的:一旦官方Raspbian升级到Jessie,DietPi也会随之切换到Jessie.由于改动较大,很多功能需要修改后才能正常使用,到时候可 ...

  2. JavaScript的Dom操作

    两个练习: 1.两个表单里面的数据相互移动: <style type="text/css"> select { width: 100px; position: rela ...

  3. android中导入低版本project可能会遇到的编译问题(转自: Victor@Beijing)

    使用高版本的SDK后再导入以前用低版本的project时,会遇到一些兼容性的问题. (1)Unable to resolve target 'android-5' 因为本机中现在使用的是2.2的SDK ...

  4. Win10 磁盘占用 100% 有效解决办法

    立即查看 任务管理器,看看是不是有一个 服务主机 unistack服务组或者找类似名称的,点开以后你会看到里面有同步主机 blablah请你毫不犹豫的结束它!结束它!结束它! 按下WIN+R调出运行, ...

  5. 详解SQL盲注测试高级技巧

    原文地址: http://www.freebuf.com/articles/web/30841.html

  6. qq空间等闪动的文字怎么做?

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. [Algorithm] 机器学习算法常用指标总结

    考虑一个二分问题,即将实例分成正类(positive)或负类(negative).对一个二分问题来说,会出现四种情况.如果一个实例是正类并且也被 预测成正类,即为真正类(True positive), ...

  8. [Hadoop] Hadoop学习历程 [持续更新中…]

    1. Hadoop FS Shell Hadoop之所以可以实现分布式计算,主要的原因之一是因为其背后的分布式文件系统(HDFS).所以,对于Hadoop的文件操作需要有一套全新的shell指令来完成 ...

  9. Visual C++ 升级到 Visual Studio

    把项目从visual C++ MFC移到visual studio 2013有许多东西需要修改,尤其是工程本身不小的时候. 到最后一步的错误: error LNK2001: unresolved ex ...

  10. Be careful about the upper limit of integer

    在面对整数时,要留心整数溢出的情况. 在探索一个数有多少个除数的程序中,原本我们只要累加自然数到大于根号N停止即可. 不过因为计算机的整数范围的限制,我们需要判断是否i*i<(i-1)*(i-1 ...