zoukankan      html  css  js  c++  java
  • 165. Compare Version Numbers

    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

    题目含义:版本号一般表示为 2.10,3.2.3这样的只含有数字和小数点的形式。比较两个版本号的大小。
     需要注意的是,2.1代表2号大版本的第1个小版本,2.10代表2号大版本的第10个小版本,所以2.10比2.1大。同时,2.1.0和2.1代表的是同一个版本。
     1     public int compareVersion(String version1, String version2) {
     2         String[] versionOne = version1.split("\.");
     3         String[] versionTwo = version2.split("\.");
     4         int length = Math.max(versionOne.length,versionTwo.length);
     5         for (int i=0;i<length;i++)
     6         {
     7             Integer value1 = i<versionOne.length?Integer.valueOf(versionOne[i]):0;
     8             Integer value2 = i<versionTwo.length?Integer.valueOf(versionTwo[i]):0;
     9             int result = value1.compareTo(value2);
    10             if (value1.compareTo(value2) != 0)
    11             {
    12                 return result;
    13             }
    14         }
    15         return 0;       
    16     }
  • 相关阅读:
    parseInt()的用法
    报文
    express的中间件与next()
    前后端分离与前后端不分离
    jQuery中四个绑定事件的区别 on,bind,live,delegate
    TCP传输的三次握手四次挥手策略
    报文
    HTTP和HTTPS以及两者的区别
    前后端不分离与分离
    express中间件的next()方法
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7686242.html
Copyright © 2011-2022 走看看