"Leetcode string Processing" Compare Version Numbers

Source: Internet
Author: User

"Leetcode string processing" Compare Version Numbers

@author: Wepon@blog: http://blog.csdn.net/u012162613
1. Topics

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

2. Analysis Test Instructions: test instructions is very clear, is to compare the "version number" size, given the version number Version1 and Version2 is a string type, when Version1>version2, return 1, reverse 1.
This problem belongs to the details of the problem, in addition to string processing is not much more cumbersome.
problem-solving ideas: first, respectively, Version1, version2 string Press '. ' Split into multiple substrings, and each substring is converted into an integral type into a container. Finally, the size of the number of the corresponding position in the two containers is compared. Of course, they need to consider the different lengths of their cases.
Note the point:(1) Form of version: 10.23.1, 01.23, 1.2.3.4 ... and so forth .(2) string conversion to Integer, my program directly with the library function stoi ()
3. Code
Class Solution {Public:int compareversion (string version1, String version2) {vector<int> Result1=getint (        Version1);        Vector<int> Result2=getint (Version2);        int len1=result1.size ();        int len2=result2.size ();        if (LEN2&LT;LEN1) return-1*compareversion (Version2, Version1);        int i=0;                while (I<len1 && result1[i]==result2[i]) i++;            The IF (I==LEN1) {//STR1 and str2 len1 bits are equal, then look at whether the str2 bits behind Len2-len1 are all 0 to determine their size int j=len2-1;            while (J >= len1) {if (result2[j--]!=0) return-1;        } return 0;            }else{//str1 and str2 before the len1 position are not equal, directly determine the first I-bit if (result1[i]<result2[i]) return-1;        else return 1; }}private://the version string by '. '        Split into multiple, converted to integral type into the container vector<int> getInt (string version) {vector<int> result;        int len=version.size ();        int pre=0; for (int i=0;i<len;i++) {if (version[i]== '. ')      {          String str (Version.begin () +pre,version.begin () +i);                Note that this type of initialization, left closed right, that STR does not include Version[version.begin () +i] Result.push_back (Stoi (str));            pre=i+1;        }}string Str (Version.begin () +pre,version.end ());        Result.push_back (Stoi (str));    return result; }};


"Leetcode string Processing" 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.