https://leetcode.com/problems/compare-version-numbers/
Compare numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return-1, otherwise ret Urn 0.
Assume that the version strings is non-empty and contain only digits and the . character.
The . character does not represent a, decimal point and was used to separate number sequences.
For instance, was not "both and 2.5 a half" or "half-to-version three", it is the fifth Second-level revision of the S Econd first-level revision.
Here are an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
Problem Solving Ideas:
This topic, the interview should be asked first, version may have multiple '. ' It? Ask again, may there be a part of version 0? Or the end is 0 consecutive, like 13.1.0.0.0? Otherwise, you can only bury your head and write the program.
It turns out that both of these situations are available, and it's not difficult to add to the consideration. I doubt that the test is how to achieve the split () method or how.
Determine the size of the two split arrays in advance, or you can do so.
Public classSolution { Public intcompareversion (String version1, String version2) {string[] version1split= Version1.split ("\ \.")); String[] Version2split= Version2.split ("\ \.")); inti = 0; while(I < version1split.length && I <version2split.length) { if(Integer.parseint (Version1split[i]) >Integer.parseint (Version2split[i])) { return1; } if(Integer.parseint (Version1split[i]) <Integer.parseint (Version2split[i])) { return-1; } I++; } if(i = = Version1split.length && i = =version2split.length) { return0; }Else if(i = =version1split.length) { for(; i < version2split.length; i++){ if(Integer.parseint (version2split[i]) > 0){ return-1; } } return0; }Else{ for(; i < version1split.length; i++){ if(Integer.parseint (version1split[i]) > 0){ return1; } } return0; } }}
Compare Version Numbers