【LeetCode】165. Compare Version Numbers

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

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

 

将被'.'分割字段逐个取出做比较即可。

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int len1 = version1.size();
        int len2 = version2.size();
        int begin1 = 0;
        int begin2 = 0;
        int end1 = 0;
        int end2 = 0;
        while(end1 != len1 && end2 != len2)
        {
            // extract section in version1
            string sec1;
            while(end1 != len1 && version1[end1] != '.')
                end1 ++;
            if(end1 == len1)
                sec1 = version1.substr(begin1);
            else
            {
                sec1 = version1.substr(begin1, end1-begin1);
                begin1 = end1 + 1;
                end1 = begin1;
            }
            
            // extract section in version2
            string sec2;
            while(end2 != len2 && version2[end2] != '.')
                end2 ++;
            if(end2 == len2)
                sec2 = version2.substr(begin2);
            else
            {
                sec2 = version2.substr(begin2, end2-begin2);
                begin2 = end2 + 1;
                end2 = begin2;
            }
            
            // compare sec1 and sec2
            int num1 = atoi(sec1.c_str());
            int num2 = atoi(sec2.c_str());
            if(num1 > num2)
                return 1;
            if(num1 < num2)
                return -1;
        }
        if(end1 == len1 && end2 == len2)
            return 0;
        while(end1 != len1)
        {// version1 remains
            string sec1;
            while(end1 != len1 && version1[end1] != '.')
                end1 ++;
            if(end1 == len1)
                sec1 = version1.substr(begin1);
            else
            {
                sec1 = version1.substr(begin1, end1-begin1);
                begin1 = end1 + 1;
                end1 = begin1;
            }
            int num1 = atoi(sec1.c_str());
            if(num1 > 0)
                return 1;
        }
        while(end2 != len2)
        {// version2 remains
            string sec2;
            while(end2 != len2 && version2[end2] != '.')
                end2 ++;
            if(end2 == len2)
                sec2 = version2.substr(begin2);
            else
            {
                sec2 = version2.substr(begin2, end2-begin2);
                begin2 = end2 + 1;
                end2 = begin2;
            }
            int num2 = atoi(sec2.c_str());
            if(num2 > 0)
                return -1;
        }
        return 0;
    }
};

posted @ 2015-06-17 10:38  陆草纯  阅读(259)  评论(0编辑  收藏  举报