那些年我们一起处理过的IP地址


验证IP地址

题目链接
在这里插入图片描述

class Solution {
public:
    bool Ip4(string IP){
        vector<string> ip;
        split(IP, ip, '.');
        if(ip.size()!=4) return false;
        for(auto s: ip){
            if(s.size()==0||s.size()>1&&s[0]=='0'||s.size()>3) return false;
            for(auto c:s){
                if(!isdigit(c)) return false;
            }
            int num = stoi(s);
            if(num>255||num<0) return false;
        }
        return true;
    }
    bool Ip6(string IP){
        vector<string> ip;
        split(IP, ip, ':');
        if(ip.size()!=8) return false;
        for(auto s: ip){
            if(s.size()==0||s.size()>4) return false;
            for(auto c:s){
                if(c<'0'||c>'9'&&c<'A'||c>'F'&&c<'a'||c>'f') return false;
            }
        }
        return true;
    }
    void split(string IP, vector<string>& vip, char sign){
        stringstream ss(IP);
        string temp;
        while(getline(ss, temp, sign)){
            vip.push_back(temp);
        }
        if(IP.size()>0&&IP.back()==sign){
            vip.push_back({});
        }
    }
    string validIPAddress(string IP) {
        if(Ip4(IP)) return "IPv4";
        if(Ip6(IP)) return "IPv6";
        return "Neither";
    }
};

复原IP地址

题目链接
在这里插入图片描述

class Solution {
    vector<string> res;
    bool isValid(string& s, int start, int end){
        if(start > end) return false;
        if(s[start] == '0' && start != end) return false;
        int num = 0;
        for(int i = start; i <= end; i++){
            if(s[i] < '0' || s[i] > '9') return false;
            num = num * 10 + (s[i] - '0');
            if(num > 255) return false;
        }
        return true;
    }
    void backtrace(string &s, int start, int pointnum){
        if(pointnum == 3){
            if(isValid(s, start, s.size() - 1)){
                res.push_back(s);   
            }
            return;
        }
        for(int i = start; i < s.size(); i++){
            if(isValid(s, start, i)){
                s.insert(s.begin() + i + 1, '.');
                pointnum++;
                backtrace(s, i + 2, pointnum);
                pointnum--;
                s.erase(s.begin() + i + 1);
            }
            else break;
        }
    } 
public:
    vector<string> restoreIpAddresses(string s) {
        backtrace(s, 0, 0);
        return res;
    }
};

比较版本号

题目链接
在这里插入图片描述

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int p = 0, q = 0;
        int end = max(version1.size(), version2.size());
        while(p < end || q < end){
            int n1 = 0, n2 = 0;
            while(p < version1.size() && version1[p] != '.'){
                n1 = n1 * 10 + (version1[p++] - '0');
            }
            while(q < version2.size() && version2[q] != '.'){
                n2 = n2 * 10 + (version2[q++] - '0');
            }
            if(n1 > n2) return 1;
            else if(n1 < n2) return -1;
            else{
                p++;
                q++;

            }
        }
        return 0;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VioletEvergarden丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值