• 【LeetCode】165. Compare Version Numbers


    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.

    将被'.'分割字段逐个取出做比较即可。

    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int len1 = version1.size();
            int len2 = version2.size();
            int begin1 = 0;
            int begin2 = 0;
            int end1 = 0;
            int end2 = 0;
            while(end1 != len1 && end2 != len2)
            {
                // extract section in version1
                string sec1;
                while(end1 != len1 && version1[end1] != '.')
                    end1 ++;
                if(end1 == len1)
                    sec1 = version1.substr(begin1);
                else
                {
                    sec1 = version1.substr(begin1, end1-begin1);
                    begin1 = end1 + 1;
                    end1 = begin1;
                }
                
                // extract section in version2
                string sec2;
                while(end2 != len2 && version2[end2] != '.')
                    end2 ++;
                if(end2 == len2)
                    sec2 = version2.substr(begin2);
                else
                {
                    sec2 = version2.substr(begin2, end2-begin2);
                    begin2 = end2 + 1;
                    end2 = begin2;
                }
                
                // compare sec1 and sec2
                int num1 = atoi(sec1.c_str());
                int num2 = atoi(sec2.c_str());
                if(num1 > num2)
                    return 1;
                if(num1 < num2)
                    return -1;
            }
            if(end1 == len1 && end2 == len2)
                return 0;
            while(end1 != len1)
            {// version1 remains
                string sec1;
                while(end1 != len1 && version1[end1] != '.')
                    end1 ++;
                if(end1 == len1)
                    sec1 = version1.substr(begin1);
                else
                {
                    sec1 = version1.substr(begin1, end1-begin1);
                    begin1 = end1 + 1;
                    end1 = begin1;
                }
                int num1 = atoi(sec1.c_str());
                if(num1 > 0)
                    return 1;
            }
            while(end2 != len2)
            {// version2 remains
                string sec2;
                while(end2 != len2 && version2[end2] != '.')
                    end2 ++;
                if(end2 == len2)
                    sec2 = version2.substr(begin2);
                else
                {
                    sec2 = version2.substr(begin2, end2-begin2);
                    begin2 = end2 + 1;
                    end2 = begin2;
                }
                int num2 = atoi(sec2.c_str());
                if(num2 > 0)
                    return -1;
            }
            return 0;
        }
    };

  • 相关阅读:
    算法设计技巧与分析(1)二分搜索的前提——线性搜索
    感谢路过秋天以及他的部门经理
    WCF部署到IIS的一个浅水滩
    在WinForm里嵌入WPF模拟公交运行状态
    基于 WPF + Modern UI 的 公司OA小助手 开发总结
    SQL Server 上关于同一张表里的三级联动
    仿百度输入框智能提示
    初探原生js根据json数据动态创建table
    程序员的热血与梦想
    记第六次面试--功夫不负有心人
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4582600.html
Copyright © 2020-2023  润新知