• 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;
        }
    };
    

      

  • 相关阅读:
    maven项目部署到tomcat中没有classe文件的问题汇总
    Tomcat远程调试模式及利用Eclipse远程链接调试
    FastDFS 常见问题
    Linux Crontab 定时任务 命令详解
    EChart 关于图标控件的简单实用
    java 通过zxing生成二维码
    Mybatis typeAliases别名
    Mybatis 实现手机管理系统的持久化数据访问层
    Mybatis 实现传入参数是表名
    Mybatis关于like的字符串模糊处理
  • 原文地址:https://www.cnblogs.com/CodingGirl121/p/5542886.html
Copyright © 2020-2023  润新知