Compare numbers version1 and Version2.
If Version1 > Version2 return 1, if Version1 < Version2 return-1, otherwise return 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, 2.5 are not "both and a half" or "half to version three", it is the fifth Second-level revision of the SEC Ond first-level revision.
Here's an example of version numbers ordering:0.1 < 1.1 < 1.2 < 13.37
Hide tags:string
Problem Solving Ideas:
(1) Use SPLT ("\ \") ) is separated by a decimal point and then compared
(2) During the comparison, we assume that the length of the array is the same. Short-length arrays, when compared to the last element, automatically add 0 actions
The code is as follows:
public static int Version (string version1, String version2) {//uses a delimiter and then compares string[] str1=version1.split ("\ \"); String[] Str2=version2.split ("\ \"); /Get the large int Length=math.max (str1.length, str2.length) that requires a scale length of two version, and for (int i = 0; i < length; i++) {/* * The following two lines of code mean the length of the two version Set to the same level as * version1=1.1 version2=1.1.2 * Execute int num1=i<str1.length? Integer.parseint (Str1[i]): 0 After * version1=1.1.0, so convenient to compare */int num1=i<str1.length? Integer.parseint (Str1[i]): 0;int num2=i<str2.length? Integer.parseint (Str2[i]): 0;if (num1>num2) {return 1;} else if (num1<num2) {return-1;}} return 0;}
leetcode--165 Compare Version Numbers (comparison of digital versions)