1. Title
Compare version Numbers (compare version number)
2. Address of the topic
https://leetcode.com/problems/compare-version-numbers/
3. Topic content
English: Compare, Numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return-1, otherwise ret Urn 0.
Chinese: Compare two versions of Version1 and Version2, if Version1 is later than Version2, return 1 if Version1 is earlier than Version2, return 1, and other cases 0
4. Methods of Solving problems
The idea of solving the problem is to first press the two version number to "." Segment, and then compare by Segment. It is important to consider the case where a version number is identical to the first half of another version number (such as "1.0.1" in "1.0" and "1.0.1" is larger, "1.0" and "1.0.0" are the same size). The Java code is as follows:
/** * function Description:leetcode 165 - compare version numbers * Developer:tsybius2014 * Development time: September 17, 2015 */public class Solution { /** * Compare version number * @param version1 version number 1 * @param version2 version number 2 * @return */ public int compareversion (String version1, string version2) { String[] versionArray1 = Version1.split ("\ \"); string[] versionarray2 = version2.split ("\ \"); int len1 = versionarray1.length; int len2 = versionarray2.length; int len = len1 <= len2 ? len1 : len2; //Total version number section, from front to back compare position number int x1, x2; for (int i = 0; i < len; i++) { x1 = integer.parseint (Versionarray1[i]); x2 = integer.parseint (VersionArray2[i]); if (X1&NBSP;>&NBSP;X2) { return 1; } else if (X1&NBSP;<&NBSP;X2) { return -1; } } // In the case of a common version number, who has more than one version number and the extra part is not 0, whose version is updated if (len1 > &NBSP;LEN2) { for (int i = len; i < len1; i++) { if (Integer.parseint (Versionarray1[i]) > 0) { return 1; } } } else if (LEN1&NBSP;<&NBSP;LEN2) { for (int i = len; i < len2; i++) { if (Integer.parseint (Versionarray2[i]) > 0) { return -1; } } } return 0; }}
END
Leetcode:compare version Numbers-Compare edition number