[Leetcode] Compare version Numbers versions comparison

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.

This question debugging for a long time, has not wanted to search others on the Internet solution, because feel oneself can do out, change to change to finally finally passed, and then surf the internet a search, found sure and other people's method is different, small has a sense of accomplishment. My idea is: because the two version number of the number of decimal points, there may be 1 and 1.1.1 comparison, there may be invalid start 0, such as 01 and 1 is the same version, and may be invalid at the end 0, such as 1.0 and 1 is the same version. For numbers that do not have a decimal point, you can default to the last digit, while the core idea of a version number comparison is a numeric comparison of the same location, such as the example given in the topic, 1.2 and 13.37 Comparisons, we all know should show 1 and 13 comparison, 13:1 large, so the latter no longer than, such as 1.1 and 1.2 compared, the front is 1, then compare the number after the decimal point. Then the algorithm is each corresponding to take out the same position of the decimal point before all the characters, convert them to a number comparison, if different can directly get the answer, if the same, and then corresponding to take. If a number has no decimal point, the default is 0, and another comparison, which resolves the case where the end is invalid 0. The code is as follows:

classSolution { Public:    intCompareversion (stringVersion1,stringVersion2) {        intN1 =version1.size (); intN2 =version2.size (); intP1 =0, p2 =0; stringv1, v2; intD1 =0, D2 =0;  while(P1 < N1 | | P2 <n2)            {v1.clear ();  while(P1 < n1 && VERSION1[P1]! ='.') {v1.push_back (Version1[p1++]); }            ++P1; D1=atoi (V1.c_str ());            V2.clear ();  while(P2 < n2 && VERSION2[P2]! ='.') {v2.push_back (version2[p2++]); }            ++P2; D2=atoi (V2.c_str ()); if(D1 > D2)return 1; Else if(D1 < D2)return-1; }        return 0; }};

[Leetcode] Compare version Numbers versions comparison

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.