Leetcode 165 Compare Version Numbers (compare edition number) (String) (*)

Source: Internet
Author: User

translation
比较两个版本号version1和version2。如果version1大于version2返回1,如果version1小于version2返回-1,否则返回0。你可以假设版本号字符串是非空并且只包含数字和“.”字符。“.”字符不代表十进制中的点,而被用作分隔数字序列。例如,2.5不是“两个半”,也不是“差一半到三”,而是第二版中的第五个小版本。这里有一个版本号排序的示例:0.1 < 1.1 < 1.2 < 13.37
Original
CompareversionNumbers version1 andVersion2. If version1 > Version2return 1,ifVersion1 < Version2return-1, otherwisereturn 0.Assume that  the versionStrings is Non-empty and containOnly digits and  the.character. The.character does  notRepresent a decimal point and  isUsed toSeparate NumberSequences. For instance,2.5  is  not "Both and a half" or "Half to version three",it  is  the Fifth Second-level Revision of  the Second  First-level revision. Here isAn example of versionNumbers Ordering:0.1<1.1<1.2<13.37
Analysis

I was complacent when I saw the problem, because the train of thought was there immediately, however ...

What I think about is that the string is based on "." To cut, then move the number from string to int to the vector, and then compare the data in the vector.

But I didn't realize there was such a sick test case.

"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 knelt down ...

Here is the code for the string vector, which is used only as a personal note ...

vector<int> getVersionVector(string version) {    vector<int> versionV;    istringstream sstream(version);    string temp;     while (!sstream.eof()) {        ‘.‘);        versionV.push_back(stoi(temp));    }                           return versionV;}

or to see someone else's code well, next time fight!

Class Solution {public:intCompareversion (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 returnStoi (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:intCompareversion (stringVersion1,stringVersion2) {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;stringSub1 = (I1 = = B1)?"0": Version1.substr (B1, I1);stringSub2 = (I2 = = b2)?"0": Version2.substr (B2, I2);intII1 = 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; }};
intCompareversion (stringVersion1,stringVersion2) {IstringstreamV1 (version1+"."), V2 (version2+'. ');CharDot ='. ';intVal1 =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&GT;VAL2)return 1;Else if(VAL1&LT;VAL2)return-1; }}

Leetcode 165 Compare version Numbers (compare version number) (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.