题解 | #比较版本号#

比较版本号

https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7

#include <sstream>
#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 比较版本号
     * @param version1 string字符串 
     * @param version2 string字符串 
     * @return int整型
     */
    int compare(string version1, string version2) {
        // write code here
        vector<int>a,b;
        stringstream ss1(version1);
        stringstream ss2(version2);
        string t;
        while(getline(ss1,t,'.')){
            a.push_back(stoi(t));
        }
        while(getline(ss2,t,'.')){
            b.push_back(stoi(t));
        }
        int i,j;
        for(i=0,j=0;i<a.size()&&j<b.size();)
        {
            if(a[i]>b[j]) return 1;
            else if(a[i]<b[j]) return -1;
            else{
                i++;j++;
            }
        }
        if(i<a.size()){
            while(i!=a.size()){
                if(a[i++]!=0) return 1;
            }
        }
        if(j<b.size()) 
            while(j!=b.size()){
                if(b[j++]!=0) return -1;
            }
        return 0;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
mjasjon:这种trash中厂 简历过筛概率比大厂还低(除阿里系)
投递哔哩哔哩等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务