前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode: Compare Version Numbers

Leetcode: Compare Version Numbers

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-25 14:44:12
3910
发布2019-01-25 14:44:12
举报

题目: 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

思路分析: 先将version字符串以点号分割放入vector中,然后将vector中的每个字符string类型转成int类型,然后依次进行int的比较。

C++示例代码:

class Solution 
{
private:
    /*
    将str以ch进行分割,结果保存在vector中返回
    */
    vector<string> split(string str, char ch)
    {
        vector<string> result;
        int index = 0;
        int count = 0;
        string substr;
        for (int i = 0; i < str.length(); i++)
        {
            if (str[i] == ch)
            {
                count = i - index;
                substr = str.substr(index, count);
                index = i + 1;
                result.push_back(substr);
            }
        }
        count = str.length()- index;
        substr = str.substr(index, count);
        result.push_back(substr);
        return result;
    }

    /*
    将vector中的string类型转换成int类型
    这里使用到了库函数stoi()
    */
    vector<int> toInt(vector<string> numbers)
    {
        int size = numbers.size();
        vector<int> intnums;
        intnums.reserve(size);
        for (int i = 0; i < size; i++)
        {
            intnums.push_back(stoi(numbers[i]));
        }
        return intnums;
    }

public:
    int compareVersion(string version1, string version2)
    {
        vector<string> strversion1 = split(version1, '.');
        vector<int> intversion1 = toInt(strversion1);

        vector<string> strversion2 = split(version2, '.');
        vector<int> intversion2 = toInt(strversion2);

        int size1 = intversion1.size();
        int size2 = intversion2.size();
        int size = size1;

        /*
        下面的ifelse是将两个vector的个数补齐,即使vector的  size是一样的,不足的补零。
        */
        if (size1 > size2)
        {
            size = size1;
            for (int i = size2; i < size; i++)
            {
                intversion2.push_back(0);
            }
        }
        else if (size1 < size2)
        {
            size = size2;
            for (int i = size1; i < size; i++)
            {
                intversion1.push_back(0);
            }
        }



        for (int i = 0; i < size; i++)
        {
            if (intversion1[i] > intversion2[i])
            {
                return 1;
            }
            else if (intversion1[i] < intversion2[i])
            {
                return -1;
            }
        }

        return 0;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年03月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档