Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created February 26, 2015 19:14
Show Gist options
  • Save dmnugent80/f8682ff08040492997f4 to your computer and use it in GitHub Desktop.
Save dmnugent80/f8682ff08040492997f4 to your computer and use it in GitHub Desktop.
Compare Version Numbers
public class Solution {
public int compareVersion(String version1, String version2) {
int retval = 0;
String[] strArray1 = version1.split("[.]");
String[] strArray2 = version2.split("[.]");
for (int i = 0; i < Math.max(strArray1.length, strArray2.length); i++){
int val1 = (i < strArray1.length ? Integer.parseInt(strArray1[i]) : 0);
int val2 = (i < strArray2.length ? Integer.parseInt(strArray2[i]) : 0);
if (val1 > val2){
return 1;
}
else if (val1 < val2){
return -1;
}
}
return retval;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment