Compare Version Numbers

Source: Internet
Author: User

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

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.