Created
February 26, 2015 19:14
-
-
Save dmnugent80/f8682ff08040492997f4 to your computer and use it in GitHub Desktop.
Compare Version Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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