2015年7月2日星期四

LeetCode: Compare Version Numbers

First try:
Just separate version into two strings by '.'
Then use stoi() function transit those strings to integer.
Well, not too bad for inputs in example of the problem.
However, when input become
"0.9.9.9.9.9.9.9", "1.0"
Totally not work! Orz. Why not explain more in the problem?

Here is a tip: stoi() is a dangerous function. It will cause core dump when input is not in a good format.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
    int compareVersion(string version1, string version2) {
         string a1, a2, b1, b2;
        bool s1,s2;
        s1=false;
        s2=false;
        for(int i=0;i<version1.length();i++){
            if(version1[i] == '.') {
                s1=true;
                continue;
            }
            if(!s1) a1+=version1[i];
            else a2+=version1[i];
        }
        for(int i=0;i<version2.length();i++){
            if(version2[i] == '.') {
                s2=true;
                continue;
            }
            if(!s2) b1+=version2[i];
            else b2+=version2[i];
        }
        int c1,c2,d1,d2;
        c1=stoi(a1);
        if(a2.empty()) c2=0; else  c2=stoi(a2);
        d1=stoi(b1);
        if(b2.empty()) d2=0; else d2=stoi(b2);
  //test
  /*cout<<"a1 "<<a1<<" "<<c1<<endl;
  cout<<"a2 "<<a2<<" "<<c2<<endl;
  cout<<"b1 "<<b1<<" "<<d1<<endl;
  cout<<"b2 "<<b2<<" "<<d2<<endl;*/
        if(c1>c2) return 1;
        if(c1<c2) return -1;
        if(d1>d2) return 1;
        if(d1<d2) return -1;
        return 0;   
    }
};


Second try:
This problem needs a split() to separate strings.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
    int compareVersion(string version1, string version2) {
        vector<int> v1, v2;
        int size1=version1.size();
        int size2=version2.size();
        for(int i=0; i<size1; i++){
            int sum=0;
            while( version1[i]!='.' ){
                sum=sum*10+version1[i]-'0';
                i++;
                if(i>=size1) break;
            }
            v1.push_back(sum);
        }
        for(int i=0; i<size2; i++){
            int sum=0;
            while( version2[i]!='.'){
                sum=sum*10+version2[i]-'0';
                i++;
                if(i>=size2) break;
            }
            v2.push_back(sum);
        }
        int a,b;
        for(int i=0; i<v1.size() || i<v2.size(); i++){
            /*int a,b;
            if(i<v1.size()) a=v1[i];
            else a=0;
            if(i<v2.size()) b=v2[i];
            else b=0;*/
            //make codes above simple
            a=(i<v1.size())?v1[i]:0;
            b=(i<v2.size())?v2[i]:0;
            if(a>b) return 1;
            if(a<b) return -1;
         }
        return 0;
    }
};

Third try:
Keep comparison when read vector.
Maybe write it later...



另附上haoel大大写的C++代码. 他封装了一个C++的split方法(C++库没有哦), 很是经典.
    vector<int> &split(const string &s, char delim, vector<int> &elems) {
        stringstream ss(s);
        string item;
        while(getline(ss, item, delim)) {
            elems.push_back(atoi(item.c_str()));
        }
        return elems;
    }


Reference: http://www.bkjia.com/cjjc/953392.html









没有评论:

发表评论