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 public class Solution { 2 public int compareVersion(String version1, String version2) { 3 String ver1_ch[] = version1.split("\."); 4 String ver2_ch[] = version2.split("\."); 5 6 int i = 0; 7 for(; i < ver1_ch.length && i <ver2_ch.length; i++){ 8 9 if(Integer.valueOf(ver1_ch[i]) > Integer.valueOf(ver2_ch[i])) 10 return 1; 11 else if(Integer.valueOf(ver1_ch[i]) < Integer.valueOf(ver2_ch[i])) 12 return -1; 13 } 14 if(i >= ver1_ch.length && i >= ver2_ch.length) 15 return 0; 16 else if(i >= ver1_ch.length){ 17 for(; i < ver2_ch.length; i++){ 18 if(Integer.valueOf(ver2_ch[i]) > 0) 19 return -1; 20 } 21 return 0; 22 } 23 else 24 { 25 for(; i < ver1_ch.length; i++){ 26 if(Integer.valueOf(ver1_ch[i]) > 0) 27 return 1; 28 } 29 return 0; 30 } 31 } 32 }