[LeetCode] 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.

 

Hide Tags
 String
 
 
分析:将字符串中的每个数字提取出来,保存到数组中,然后逐个比较数组各个元素。
 
注意:“1” 和 “1.0” 以及“1.0.0” 是相同的,需要单独处理一下。。

 

也快参考 http://www.cnblogs.com/grandyang/p/4244123.html 

 

class Solution {
    private:
        int string2Int(const string& str)
        {
            if(str.size() == 0)
                return 0;

            int rtn = 0;
            for(int i = 0; i < str.size(); i++)
            {
                rtn = rtn*10 + (str[i] - '0');
            }
            return rtn;
        }

        vector<int> string2Ints(const string& str)
        {
            vector<int> rtn;
            if(str.size() == 0)
                return rtn;

            int size = str.size();
            int left = 0;

            int tmp;
            for(int i = 0; i < size; i++)
            {
                if(str[i] == '.')
                {
                    tmp = string2Int(str.substr(left, i - left ));
                    rtn.push_back(tmp);
                    left = i + 1;
                }
            }
            tmp = string2Int(str.substr(left, size + 1 - left ));
            rtn.push_back(tmp);
            return rtn;

        }
    public:
        int compareVersion(string version1, string version2)
        {
            if(version1 == version2)
                return false;
            vector<int> rtn1 = string2Ints(version1);
            vector<int> rtn2 = string2Ints(version2);

            //printVector(rtn1);
            //printVector(rtn2);
            int size = min(rtn1.size(), rtn2.size());
            for(int i = 0; i < size; i++)
            {
                if(rtn1[i] > rtn2[i] )
                    return 1;
                else if(rtn1[i] < rtn2[i] )
                    return -1;
            }
            if(rtn1.size() == rtn2.size())
                return 0;
            else if(rtn1.size() > rtn2.size())
            {
                for(int i = size; i < rtn1.size(); i++)
                {
                    if(rtn1[i] != 0)
                        return 1;
                }
                return 0;//all is zero
            }
            else //if(rtn1.size() < rtn2.size())
            {
                for(int i = size; i < rtn2.size(); i++)
                {
                    if(rtn2[i] != 0)
                        return -1;
                }
                return 0;//all is zero
            }
        }
};

 

posted @ 2015-07-10 11:31  穆穆兔兔  阅读(184)  评论(0编辑  收藏  举报