Compare two version numbers version1 and version1.
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.

给2个版本的字符串,里面只含数字和 '.' ,比较两个版本的大小。

解法:对字符串以 '.' ,进行分拆形成数组。然后从数组的第一个元素开始比较,如果相同就比较下一个元素,如果不同v1大就返回1,v2 大就返回-1,如果比较到最后都相同就返回0。

注意:如果产生的两个数组长度不一样,例如:1.2 < 1.2.3 则要把短的后面加上'0'补齐数组长度和长的一样,然后在进行比较。

Java:

    String[] arr1 = version1.split("\\.");
String[] arr2 = version2.split("\\."); int i=0;
while(i<arr1.length || i<arr2.length){
if(i<arr1.length && i<arr2.length){
if(Integer.parseInt(arr1[i]) < Integer.parseInt(arr2[i])){
return -1;
}else if(Integer.parseInt(arr1[i]) > Integer.parseInt(arr2[i])){
return 1;
}
} else if(i<arr1.length){
if(Integer.parseInt(arr1[i]) != 0){
return 1;
}
} else if(i<arr2.length){
if(Integer.parseInt(arr2[i]) != 0){
return -1;
}
} i++;
} return 0;
}

Java:

public class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) return 0;
String[] v1s = version1.split("\\.");
String[] v2s = version2.split("\\.");
int i = 0, j = 0, res = 0;
while (i < v1s.length || j < v2s.length) {
int ver1 = i < v1s.length ? Integer.valueOf(v1s[i++]) : 0;
int ver2 = j < v2s.length ? Integer.valueOf(v2s[j++]) : 0;
if (ver1 < ver2) return -1;
else if (ver1 > ver2) return 1;
}
return 0;
}
}

Python:

class Solution2(object):
def compareVersion(self, version1, version2):
"""
:type version1: str
:type version2: str
:rtype: int
"""
v1, v2 = version1.split("."), version2.split(".") if len(v1) > len(v2):
v2 += ['0' for _ in xrange(len(v1) - len(v2))]
elif len(v1) < len(v2):
v1 += ['0' for _ in xrange(len(v2) - len(v1))] i = 0
while i < len(v1):
if int(v1[i]) > int(v2[i]):
return 1
elif int(v1[i]) < int(v2[i]):
return -1
else:
i += 1 return 0

Python: My solution  

class Solution(object):
"""
:type v1, str
:type v2, str
:rtype: int
"""
def compareVersion(self, v1, v2):
split_v1 = v1.split('.')
split_v2 = v2.split('.')
print split_v1, split_v2
n1 = len(split_v1)
n2 = len(split_v2) if n1 < n2:
for i in xrange(n2 - n1):
split_v1 += '0'
elif n1 > n2:
for i in xrange(n1 - n2):
split_v2 += '0' for i in xrange(len(split_v1)):
if split_v1[i] == split_v2[i]:
continue
if split_v1[i] > split_v2[i]:
return 1
if split_v1[i] < split_v2[i]:
return -1 return 0

类似题目:

[LeetCode] 278. First Bad Version 第一个坏版本

  

All LeetCode Questions List 题目汇总

[LeetCode] 165. Compare Version Numbers 比较版本数的更多相关文章

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

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

  2. Java for LeetCode 165 Compare Version Numbers

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

  3. Java [Leetcode 165]Compare Version Numbers

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

  4. Leetcode 165 Compare Version Numbers

    题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...

  5. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

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

  6. 165. Compare Version Numbers - LeetCode

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

  7. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  8. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  9. 【LeetCode】165 - Compare Version Numbers

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

随机推荐

  1. php string常用函数

    <?php $a[]='a'; $a[]='b'; $a[]='C'; echo "</br>"; /* implode — 将一个一维数组的值转化为字符串 说明 ...

  2. 项目alpha冲刺-测试

    作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及可视化分析平台 这个作业的 ...

  3. intellij高亮字体背景颜色

    https://blog.csdn.net/aosica321/article/details/52956419 https://blog.csdn.net/lxzpp/article/details ...

  4. 关于原生js的节点兼容性

    关于节点的兼容性: 1:获取元素的子节点 a: childNodes:获取元素的子节点,空文本,非空文本,注释,获取的比较全面, 如果只是想获取元素的子节点,请用(children) b:     c ...

  5. RedisTemplate在项目中的应用

    如下主要通去年无聊做的 "涂涂影院后台管理系统" 一个 demo,看 RedisTemplate 的使用. 体验地址:http://video.71xun.com:8080  账户 ...

  6. python -- 连接 orclae cx_Oracle的使用 二

    转:https://www.cnblogs.com/cyxiaer/p/9396861.html 必需的Oracle链接库的下载地址:https://www.oracle.com/technetwor ...

  7. SpringBoot第三节(thymeleaf的配置与SpringBoot注解大全)

    Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.所以这里介绍一下Springboot使用Thymeleaf的实例以及遇到的问题. 1.配置与使用 1.1:在applica ...

  8. WinDbg常用命令系列---!htrace

    !htrace 简介 !htrace扩展显示一个或多个句柄的堆栈跟踪信息. 使用形式 用户模式!htrace [Handle [Max_Traces]] !htrace -enable [Max_Tr ...

  9. 洛谷P4047 [JSOI2010]部落划分题解

    洛谷P4047 [JSOI2010]部落划分题解 题目描述 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落 ...

  10. c++中二叉树的先序中序后序遍历

    c++中二叉树的先(前)序.中序.后序遍历 讲解版 首先先看一个遍历的定义(源自度娘): 所谓遍历(Traversal),是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问.访问结点所做的 ...