// 方法一 classSolution{ publicintcompareVersion(String version1, String version2){ String[] v1 = version1.split("\\."); String[] v2 = version2.split("\\."); for(int i = 0; i < v1.length || i < v2.length; i++) { int x = 0, y = 0; if(i < v1.length){ x = Integer.parseInt(v1[i]); } if(i < v2.length){ y = Integer.parseInt(v2[i]); } if(x > y) return1; if(x < y) return -1; } return0; } }
// 方法二 classSolution{ publicintcompareVersion(String version1, String version2){ int m = version1.length(); int n = version2.length(); int i = 0, j = 0; while(i < m || j < n){ int x = 0; for(;i < m && version1.charAt(i) != '.'; i++){ x = 10 * x + version1.charAt(i) - '0'; } i++; int y = 0; for(;j < n && version2.charAt(j) != '.'; j++){ y = 10 * y + version2.charAt(j) - '0'; } j++; if(x != y){ return x > y ? 1 : -1; } } return0; } }