首页 > 其他 > 详细

LeetCode 165.Compare Version Numbers

时间:2016-08-06 20:35:06      阅读:215      评论:0      收藏:0      [点我收藏+]

165. Compare Version Numbers

 
 My Submissions
 
  • Total Accepted: 61127
  • Total Submissions: 333997
  • Difficulty: Easy

 

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


这个题目的难点在于其各种特殊情况,如000.0和0,返回值是0,001和1返回值也是0,10.1和10.1.0返回值也是0

我解决的办法是找到.这个分界点如果比出来了就可以得到返回值,如果前面的值的大小是相同的,则递归调用,传递的参数是去除前面的相等的数据之后的字符串

class Solution {
public:
    int compareVersion(string version1, string version2) {
        
        size_t pos1 = 0, pos2 = 0;
        int num1 = 0, num2 = 0;
        if ((count(version1.cbegin(), version1.cend(), 0) + count(version1.cbegin(), version1.cend(), .)) ==
            version1.length())
        {
            if (count(version2.cbegin(), version2.cend(), 0) + count(version2.cbegin(), version2.cend(), .) ==
                version2.length())
                return 0;
            else return -1;
        }
        else
        {
            if (count(version2.cbegin(), version2.cend(), 0) + count(version2.cbegin(), version2.cend(), .) ==
                version2.length())
                return 1;
            else
            {
                pos1 = version1.find_first_not_of(0);
                pos2 = version2.find_first_not_of(0);
                version1 = version1.substr(pos1);
                version2 = version2.substr(pos2);
                if (version1 == version2)return 0;
                pos1 = version1.find_first_of(.);
                pos2 = version2.find_first_of(.);
                if (pos1 != 0&&pos1!=string::npos)
                num1 = stoi(version1.substr(0,pos1));
                else if (pos1 == string::npos)
                {
                    num1 = stoi(version1.substr(0));
                }
                else num1 = 0;
                if(pos2!=0&&pos2!=string::npos)
                num2 = stoi(version2.substr(0,pos2));
                else if (pos2 == string::npos)
                {
                    num2 = stoi(version2.substr(0));
                }
                else num2 = 0;
                if (num1 == num2&&pos1 == string::npos&&pos2 == string::npos)return 0;
                if (num1 == num2&&pos1 == string::npos)
                {
                    version2 = version2.substr(pos2);
                    if ((count(version2.cbegin(), version2.cend(), .) + count(version2.cbegin(), version2.cend(), 0)) == version2.length())
                        return 0;
                    else return -1;
                }
                if (num1 == num2&&pos2 == string::npos)
                {
                    version1 = version1.substr(pos1);
                    if ((count(version1.cbegin(), version1.cend(), .) + count(version1.cbegin(), version1.cend(), 0)) == version1.length())
                        return 0;
                    else return 1;
                }
                if (num1 == num2&&num1==0)return compareVersion(version1.substr(pos1+1), version2.substr(pos2+1));
                if (num1 == num2&&num1 != 0)return compareVersion(version1.substr(pos1), version2.substr(pos2));
                else
                {
                    if (num1 > num2)return 1;
                    if (num1 < num2)return -1;
                }
            }
        }
        return 0;
    }
};

 

LeetCode 165.Compare Version Numbers

原文:http://www.cnblogs.com/csudanli/p/5744777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!