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     }
原文地址:https://www.cnblogs.com/wzj4858/p/7686242.html