• [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

    Credits:
    Special thanks to @ts for adding this problem and creating all test cases.

    Hide Tags
     String
     
      这题是太多细节的操作,字符串string 的操作其实很简单,主要判断0的情况,需要考虑1.1.0 与1.1 是相等的。代码写的有点冗余,如果再第一个while 中修改可以改短。
     
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            if (version1.length()<1||version2.length()<1)   return 0;
            int beg1 = 0,beg2 = 0;
            int end1 = 0;
            int end2 = 0;
    
            while(end1!=string::npos&&end2!=string::npos){
                end1 = version1.find_first_of('.',beg1);
                end2 = version2.find_first_of('.',beg2);
                int int1 = hFun(version1.substr(beg1,end1-beg1));
                int int2 = hFun(version2.substr(beg2,end2-beg2));
                if(int1 > int2) return 1;
                if(int1 < int2) return -1;
                beg1 = end1 +1;
                beg2 = end2 +1;
            }
            if(end1==string::npos){
                while(end2!=string::npos){
                    end2 = version2.find_first_of('.',beg2);
                    int int2 = hFun(version2.substr(beg2,end2-beg2));
                    if(int2 >0) return -1;
                    beg2 = end2+1;
                }
            }
            if(end2==string::npos){
                while(end1!=string::npos){
                    end1 = version1.find_first_of('.',beg1);
                    int int1 = hFun(version1.substr(beg1,end1-beg1));
                    if(int1 >0) return 1;
                    beg1 = end1+1;
                }
            }
            return 0;
        }
        int hFun(string s)
        {
            int n = 0;
            for(int i=0;i<s.length();i++){
                n*=10;
                n+=s[i]-'0';
            }
            return n;
        }
    };
    
    int main()
    {
        string version1 = "1.1";
        string version2 = "1";
        Solution sol;
        cout<<sol.compareVersion(version1,version2)<<endl;
        return 0;
    }
     
     
     
      
  • 相关阅读:
    webpack优化
    原生js实现平滑滚动
    【大前端攻城狮之路】JavaScript函数式编程
    【大前端攻城狮之路】面试集锦
    【大前端攻城狮之路·二】Javascript&QA⼯程师
    【vue系列之三】从一个vue-pdf-shower,说说vue组件和npm包
    display:box和display:flex填坑之路
    【vue系列之一】使用vue-cli脚手架工具搭建vue-webpack项目
    快速上手微信小程序-快递100
    js面向对象小结(工厂模式,构造函数,原型方法,继承)
  • 原文地址:https://www.cnblogs.com/Azhu/p/4323837.html
Copyright © 2020-2023  润新知