[C + +] leetcode:59 Compare Version Numbers

Source: Internet
Author: User

Topic:

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,2.5is 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

Answer 1: C language method

Ideas:

Use the Strtol function automatically according to the delimiter '. ' Separate the version numbers and compare them to each other until they are not equal.

Background knowledge:

1. Strtol

Long int strtol (const char *nptr,char **endptr,int base);

converts the number of growth integers to the parameter nptr string based on the parameter base. Base indicates the number of digits, such as 10, that is decimal. The strtol scan skips the preceding space character until it encounters a number or sign, and then ends the conversion with a non-numeric or string terminator, "\" , and returns the result.

endptr is an outgoing parameter, and when the function returns, it points to the first character that is not recognized later

2. CString and string differences

cstring is an array of characters terminated with null characters. string class is the class of the standard library, not the built-in type, the standard library is similar to the class we define ourselves, and the string object does not end with the standard. However, string can be converted automatically.

Attention:

1. Note '. ' The role is to differentiate the version number, so the following version number will appear, containing multiple '. '.   such as "12.13.24.0" you must differentiate and compare numbers by delimiter.

2. Note If two version number lengths are inconsistent, the missing should be 0 compared. such as "12.3.34 and 12.3" <=> "12.3.34 and 12.3.0"

3. Note The pointer operation, strtol return parameters, directly change the C-style string pointer, pointing to the delimiter.  

Long V1 = *VSTR1 = = = ' + '? 0:strtol (VSTR1, &VSTR1, 10);
Complexity: O (N)

AC Code:

Class Solution {public:    int compareversion (string version1, String version2) {        int ret = 0;                Converts a string into a C-style character array        char* vStr1 = (char*) version1.c_str ();        char* vStr2 = (char*) version2.c_str ();                while (ret = = 0 && (*vstr1! = ' *vstr2 ')        {            //strtol will scan the parameter nptr string, skipping the preceding space character, The conversion is not started until a number or sign is encountered, and then the conversion is ended with a non-numeric or string ending (' + '), and the result is returned. The second parameter is an outgoing parameter, which, when returned, points to the first character that is not recognized, that is, '. '            Long V1 = *VSTR1 = = = ' + '? 0:strtol (VSTR1, &VSTR1, ten);            Long v2 = *VSTR2 = = = ' + '? 0:strtol (VSTR2, &VSTR2, ten);            if (V1 > V2)                ret = 1;            else if (V1 < v2)                ret =-1;            else            {                if (*vstr1! = ') ' vstr1++;                if (*vstr2! = ') ') vstr2++;            }        }                return ret;            }};

Answer2:c++ method

idea: Use the split function directly according to the delimiter '. ' Separate the version number into the array, and then compare each. Use the Split function in the Boost library.

#include <boost/algorithm/string.hpp>STD::Vector<STD::string>STRs;Boost::Split(STRs, "string to split",Boost::is_any_of("\ T"));
Code:leetcode seems to have no way to call the boost library.

Line 1: ' Boost ' isn't a namespace-name

Using namespace Boost;class Solution {public:    int compareversion (string version1, String version2) {        int ret = 0;< C2/>if (version1.size () = = 0 | | version2.size () = = 0) return ret;                Vector<string> v1;        Vector<string> v2;                Boost::split (v1, Version1, boost::is_any_of ("."));        Boost::split (v2, Version2, Boost::is_any_of ("."));                int len1 = V1.size ();        int len2 = V2.size ();                int i = 0;        for (ret = = 0 && (i < len1 | | I < LEN2))        {            int x1 = i < len1? Stoi (V1[i]): 0            ; int x2 = i < len2? Stoi (V2[i]): 0;            if (X1 > x2)                ret = 1;            else if (X1 < x2)                ret =-1;            else              i++;          }                return ret;    }};

[C + +] leetcode:59 Compare Version Numbers

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.