Leetcode:compare Version Numbers

Source: Internet
Author: User

Compare numbers version1 and version1.
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

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

Approximate test instructions: Compare the version number, the two elements of the comparison are composed of a comma split string

intCompareversion (stringVersion1,stringVersion2) {Version1+='.'; Version2+='.'; intL1=version1.length (), l2=version2.length (), i,j,num1,num2; I=0; J=0;  while(I<l1 | | j<L2) {NUM1=0;  while(I<l1 && version1[i]!='.') {NUM1=num1*Ten+version1[i]-'0'; ++i; }        if(version1[i]=='.') ++i; Num2=0;  while(J<l2 && version2[j]!='.') {num2=num2*Ten+version2[j]-'0'; ++J; }        if(version2[j]=='.') ++J; if(num1!=num2)returnNum1>num2?1:-1; }    return 0; }

The interesting thing about this problem is the unified approach.

First in the string Wingarton, so that each time can be comma as a criterion

Second while (i<l1 | | j<L2) This sentence is also very interesting, I began to write the while (I<l1 && j<l2), In this case, each time the string length is different to determine the processing after the while

if return -1;     Else if return 1;

Because the front are all the same, not over the string affirmation version of the new, so glorious WA, there is data is 1 1.0,23333

So at the end of a string, whether or not the string is 0, but also to see, here just need to change the cycle conditions on the line

Leetcode: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.