zoukankan      html  css  js  c++  java
  • 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

    题目解答:

      题目要求对输入的两个版本号,比较其大小。

      思路:(1)将版本号按照.号分隔。得到字符串向量。

         (2)将字符串转成整数值比较大小。

      需要注意的是几个特殊的例子需要考虑: 1.0.0 vs 1 这个结果应该是相等的。

    代码:

    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            if((version1 == "") || (version2 == ""))
                return 0;
            vector<int> v1_vec = getVersionNum(version1);
            vector<int> v2_vec = getVersionNum(version2);
            auto vit1 = v1_vec.begin();
            auto vit2 = v2_vec.begin();
            while((vit1 != v1_vec.end()) && (vit2 != v2_vec.end() ) )
            {
                if(*vit1 < *vit2)
                {
                    return -1;
                }
                else if(*vit1 > *vit2)
                {
                    return 1;
                }
                else
                {
                    vit1++;
                    vit2++;
                }
            }
            if( (vit1 == v1_vec.end()) && (vit2 != v2_vec.end()) )
            {
                int leave = 0;
                while(vit2 != v2_vec.end() )
                {
                    leave += *vit2;
                    vit2++;
                }
                if(leave > 0)
                    return -1;
                else
                    return 0;
            }
            else if( (vit1 != v1_vec.end()) && (vit2 == v2_vec.end() ))
            {
                int leave = 0;
                while(vit1 != v1_vec.end() )
                {
                    leave += *vit1;
                    vit1++;
                }
                if(leave > 0)
                    return 1;
                else
                    return 0;
            }
            else
                return 0;
            
        }
        
        
        vector<int> getVersionNum(string s)
        {
            vector<int> res;
            vector<string> sve = stringSplit(s);
            for(auto sit = sve.begin(); sit != sve.end(); sit++)
            {
                
                int num_tmp = 0;
                num_tmp = atoi((*sit).c_str());
                cout << num_tmp << endl;
                res.push_back(num_tmp);
            }
            return res;
        }
        
        
        
        vector<string> stringSplit(string s)
        {
            vector<string> res;
            string::iterator sit = s.begin();
            string tmp;
            tmp.clear();
            while(sit != s.end())
            {
                if(*sit == '.')
                {
                    res.push_back(tmp);
                    tmp.clear();
                }
                else
                {
                    tmp += *sit; 
                }
                sit++;
            }
            res.push_back(tmp);
           // cout << tmp << endl;
            tmp.clear();
            return res;
        }
    };
    

      

  • 相关阅读:
    表达式计算 六月飞雪
    code::blocks 单步执行 六月飞雪
    5.1 字符串 六月飞雪
    对使用倒序的一维数组解决0/1背包问题的理解 六月飞雪
    5.2 高精度运算 六月飞雪
    关于ArcEngine“不能再打开其他表了”的错误 (20121026 15:43:33)
    关于AO插入对象
    多线程使用实例
    C#程序运行时间长出现无法响应状态
    Geographic coordinate system和projected coordinate
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5542886.html
Copyright © 2011-2022 走看看