Java-compare Version Numbers

Source: Internet
Author: User

Compare numbers version1 and version1.
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 is not "both and a half" or "half-to-version three", it is the fifth Second-level revision of the second first-level re Vision.

Here are an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Compare the old and new version numbers

To. Compare each for the bounds in turn. The number of sizes before the size of the output equals on the continuation of special cases is similar to 1 with 1.0 at this time also think of their equal code as follows;

public class Solution {public static int compareversion (string version1, String version2) {return compare (Version1, 0,    Version2, 0);} public static int Compare (string Version1,int st1, String version2,int st2) {int SSA = 1;int SSB = 1;if (St1 = = VERSION1.L Ength () | | St1 = = Version1.length () + 1) {SSA = 0;if (St2 = = Version2.length () | | st2 = version2.length () + 1) {return 0;}} if (St2 = = Version2.length () | | st2 = version2.length () + 1) {SSB = 0;if (St1 = = Version1.length () | | st1 = Version1.len Gth () + 1) {return 0;}} int i = St1;int a=0;if (SSA! = 0) {String S1 = ""; I < version1.length (); i++) {if (Version1.charat (i) = = '. ') Break;elses1 = S1 + version1.charat (i);} A = integer.valueof (S1);} Int J = St2;int B=0;if (SSB! = 0) {String s2 = ""; J < Version2.length (); J + +) {if (Version2.charat (j) = = '. ') Break;elses2 = s2 + Version2.charat (j);} b = integer.valueof (s2);} if (a > B) return 1;else {if (a < b) Return-1;else {if (ssa==0| | ssb==0) return 0;else return CoMpare (Version1, i + 1, Version2, J + 1);}}} 


Java-compare Version Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.