LeetCode 165 Compare Version Numbers (comparison Version) (string )(*)

Source: Internet
Author: User

LeetCode 165 Compare Version Numbers (comparison Version) (string )(*)
Translation

Compare two versions: version1 and version2. If version1 is greater than version2, 1 is returned. If version1 is less than version2,-1 is returned. Otherwise, 0 is returned. You can assume that the version number string is not empty and only contains numbers and "." characters. The "." character is used to separate numeric sequences instead of decimal vertices. For example, 2.5 is neither a "Two Half" nor a "half to three", but the fifth minor version in the second version. Here is an example of version number sorting: 0.1 <1.1 <1.2 <13.37
Original
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and contain only digits and the . character.The . character does not represent a decimal point and is used to separate number sequences.For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.Here is an example of version numbers ordering:0.1 < 1.1 < 1.2 < 13.37
Analysis

I was also complacent when I saw this question, because the idea was ready immediately, ......

What I think of is to cut the string according to ".", then convert the number from string to int and save it to the vector, and finally compare the data in the vector.

But I didn't expect such abnormal test cases ......

"19.8.3.17.5.01.0.0.4.0.0.0.0.0.0.0.0.0.0.0.0.0.00.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.000000.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.000000""19.8.3.17.5.01.0.0.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0000.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.000000"

Then I got down ......

Below is the code for converting string to vector, which is used only for personal notes ......

vector
  
    getVersionVector(string version) {    vector
   
     versionV;    istringstream sstream(version);    string temp;     while (!sstream.eof()) {        getline(sstream, temp, '.');        versionV.push_back(stoi(temp));    }                           return versionV;}
   
  

Let's take a look at other people's code and try again next time!

class Solution {public:    int compareVersion(string version1, string version2) {        istringstream s1(version1), s2(version2);        string a, b;        while (!s1.eof() && !s2.eof())        {            getline(s1, a, '.');            getline(s2, b, '.');            if (stoi(a) == stoi(b)) continue;            else return stoi(a) > stoi(b) ? 1 : -1;        }        if (s1.eof() && s2.eof()) return 0;        if (s1.eof())        {            while (!s2.eof())            {                getline(s2, b, '.');                if (stoi(b) != 0) return -1;            }        }        if (s2.eof())        {            while (!s1.eof())            {                getline(s1, a, '.');                if (stoi(a) != 0) return 1;            }        }        return 0;    }};
class Solution {public:    int compareVersion(string version1, string version2) {        size_t b1 = 0, i1 = 0, b2 = 0, i2 = 0;        while (i1 < version1.size() || i2 < version2.size()) {            while (i1 < version1.size() && version1[i1] != '.') ++i1;            while (i2 < version2.size() && version2[i2] != '.') ++i2;            string sub1 = (i1 == b1) ? "0" : version1.substr(b1, i1);            string sub2 = (i2 == b2) ? "0" : version2.substr(b2, i2);            int ii1 = stoi(sub1), ii2 = stoi(sub2);            if (ii1 > ii2) return 1;            else if (ii1 < ii2) return -1;            else {                b1 = (b1 == i1) ? i1 : (i1++) + 1;                b2 = (b2 == i2) ? i2 : (i2++) + 1;            }        }        return 0;    }};
int compareVersion(string version1, string version2) {    istringstream v1(version1+"."), v2(version2+'.');    char dot = '.';    int val1 = 0, val2 = 0;    while (true) {        void* p1 = (v1>>val1>>dot), *p2= (v2>>val2>>dot);        if (! p1 && !p2)            return 0;        if (! p1)             val1 = 0;        if (! p2)            val2 = 0;        if (val1>val2)            return 1;        else if (val1
  

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.