Thursday, July 23, 2015

Leetcode 165: compare version numbers


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

题目给的example没有给好,1.01.0 < 1.01.00001 =1.01.00001.0000000.0000000000
这个题目就是字符串处理的问题,题目没有什么难度,但是要注意细节的处理,这些处理起来很繁琐, 对每个由'.'隔开的字符串,进行比较大小,并且可能v1的字符串比v2的字符串长很多., 这多在最后多余计算中都要进行处理。



class Solution {
public:
    int compareVersion(string version1, string version2) {
       //first compare the num before .
       int s1 = 0;
       int s2 = 0;
       int i1 = 0;
       int i2 = 0;
       while (i1 < version1.size() && i2 < version2.size()) {
           s1 = 0;
           s2 = 0;
           for (;i1 < version1.size(); i1++) {
                if (version1[i1] != '.') {
                    s1 = s1 * 10 + version1[i1]-'0';
                } else {
                    break;
                }
           }
           for (; i2 < version2.size(); i2++) {
                if (version2[i2] != '.') {
                    s2 = s2 * 10 + version2[i2]-'0';
                } else {
                    break;
                }
           }
           if (s1 < s2) return -1;
           if (s1 > s2) return 1;
           i1++;
           i2++;
       }
       s1 = 0;
       s2 = 0;
       if (i1 < version1.size()) {
           for (; i1 < version1.size();i1++) {
               if (version1[i1] != '.') s1 = s1*10 + version1[i1]-'0';
               else break;
           }
           if (s1 != 0) return 1;

       }
       if (i2 < version2.size()) {
           for (; i2 < version2.size();i2++) {
               if (version2[i2] != '.') s2 = s2*10 + version2[i2]-'0';
               else break;
           }
           if (s2 != 0) return -1;

       }
       return 0;
    }
};

updated:

其实后面两个长度不同的时候的判断是可以直接放进循环里面的, 因为操作一样, 更新代码如下:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int i1 = 0;
        int i2 = 0;
        int s1 = 0;
        int s2 = 0;
        while (i1 < version1.size() || i2 < version2.size()) {
            s1 = 0;
            s2 = 0;
            for (; i1 < version1.size();i1++) {
                if (version1[i1] != '.') {
                    s1 *= 10;
                    s1 += version1[i1] - '0';
                } else {
                    break;
                }
            }
            for (; i2 < version2.size(); i2++) {
                if (version2[i2] != '.') {
                    s2 *= 10;
                    s2 += version2[i2] - '0';
                } else {
                    break;
                }
            }
            if (s1 < s2) return -1;
            if (s1 > s2) return 1;
            i1++;
            i2++;
        }
        return 0;
    }
};

No comments:

Post a Comment