博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 165. Compare Version Numbers 比较版本数
阅读量:4687 次
发布时间:2019-06-09

本文共 3085 字,大约阅读时间需要 10 分钟。

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  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
Integer.parseInt(arr2[i])){ return 1; } } else if(i

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

 

类似题目:

  

转载于:https://www.cnblogs.com/lightwindy/p/8643778.html

你可能感兴趣的文章
C和C++中的计时器
查看>>
LeetCode "Different Ways to Add Parentheses"
查看>>
Windows Phone SDK 7.1 简体中文版离线安装包地址
查看>>
UITableView的cell重用优化
查看>>
如何使用Java读写系统属性?
查看>>
Oracle体系结构之控制文件管理
查看>>
hibernate 多对多操作(级联操作)
查看>>
python数据分析基础
查看>>
mybatis入门截图四(订单商品数据模型-懒加载-缓存)
查看>>
判断一个js对象是否是Array
查看>>
关于Merge的整理--AndroidScreenSlidePager开源库中用到的
查看>>
五秒原则,做一件事之前数 5 秒,1,2,3,4,5 立马去做。比如睡觉:数五秒,立马放下手机,闭眼。...
查看>>
Hibernate之HQL查询
查看>>
数字类型-课堂笔记
查看>>
powerShell赋权限
查看>>
java中方法体的作用
查看>>
windows进程间通信
查看>>
linux TCP协议(1)---连接管理与状态机
查看>>
'net’ 不是内部命令或外部命令,也不是可运行的程序或批处理文件 解决方法...
查看>>
POJ 2674 Linear world(弹性碰撞)
查看>>