leetcode中Compare Version Numbers 问题

##问题描述
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

##问题分析
该问题很简单,即比较版本号的大小,主要复杂的是多种情况的比较
例如 1<1.1 01<1.1 这些特殊情况的处理是重点

##JAVA代码
public class Solution {
public int compareVersion(String version1, String version2) {
String[] num1 = version1.split(“\.”);
String[] num2 = version2.split(“\.”);
int i = 0,j = 0;
for(;i <= num1.length - 1 && j <= num2.length - 1;i++,j++){
if (Integer.parseInt(num1[i]) > Integer.parseInt(num2[j])) return 1;
else if (Integer.parseInt(num1[i]) < Integer.parseInt(num2[j])) return -1;
}
//当version1=1;version2=1.1时,return-1
//而此时num1.length=1,num2.length=2
for(;i<num1.length;i++){
if(Integer.parseInt(num1[i]) != 0) return 1;
}
for(;j<num2.length;j++){
if(Integer.parseInt(num2[j]) != 0) return -1;
}
return 0;
}
}

##代码分析
假设输入是1和1.1分析上述代码
运行到第二个for循环时 i=1 j=1,此时num1.length=1,num2.length=2
第二个for循环不会执行
第三个fo循环执行