Leetcode oj:compare version Numbers (compare versions string)

Source: Internet
Author: User

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

Note that there may be a pre-0 situation, so the decimal point is divided into a decimal point after the conversion into a number to determine the code as follows:

1 classSolution {2  Public:3     intCompareversion (stringVersion1,stringVersion2) {4         intI1, I2;5         intVal1, Val2;6          for(I1 =0, I2 =0; I1 < Version1.size () | | I2 < Version2.size (); ++I1, + +I2) {7Val1 =0;8              for(; I1 < Version1.size (); + +I1) {9                 if(Version1[i1] = ='.')Ten                      Break; OneVal1 = Val1 *Ten+ VERSION1[I1]-'0'; A             } -Val2 =0; -              for(; i2 < Version2.size (); + +I2) { the                 if(Version2[i2] = ='.') -                      Break; -Val2 = Val2 *Ten+ Version2[i2]-'0'; -             } +             if(Val1 >val2) -                 return 1; +             Else if(Val1 <val2) A                 return-1; at         } -         return 0; -     } -};

Leetcode oj:compare version Numbers (compare versions string)

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.