468. 验证IP地址

编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。

IPv4 地址由十进制数和点来表示,每个地址包含4个十进制数,其范围为 0 - 255, 用(".")分割。比如,172.16.254.1

同时,IPv4 地址内的数不会以 0 开头。比如,地址 172.16.254.01 是不合法的。

IPv6 地址由8组16进制的数字来表示,每组表示 16 比特。这些组数字通过 (":")分割。比如,  2001:0db8:85a3:0000:0000:8a2e:0370:7334 是一个有效的地址。而且,我们可以加入一些以 0 开头的数字,字母可以使用大写,也可以是小写。所以, 2001:db8:85a3:0:0:8A2E:0370:7334 也是一个有效的 IPv6 address地址 (即,忽略 0 开头,忽略大小写)。

然而,我们不能因为某个组的值为 0,而使用一个空的组,以至于出现 (::) 的情况。 比如, 2001:0db8:85a3::8A2E:0370:7334 是无效的 IPv6 地址。

同时,在 IPv6 地址中,多余的 0 也是不被允许的。比如, 02001:0db8:85a3:0000:0000:8a2e:0370:7334 是无效的。

说明: 你可以认为给定的字符串里没有空格或者其他特殊字符。

示例 1:

输入: "172.16.254.1"

输出: "IPv4"

解释: 这是一个有效的 IPv4 地址, 所以返回 "IPv4"。

示例 2:

输入: "2001:0db8:85a3:0:0:8A2E:0370:7334"

输出: "IPv6"

解释: 这是一个有效的 IPv6 地址, 所以返回 "IPv6"。

示例 3:

输入: "256.256.256.256"

输出: "Neither"

解释: 这个地址既不是 IPv4 也不是 IPv6 地址。
class Solution {
public:
    string validIPAddress(string IP) {
        if (IP.empty()) {
            return "Neither";
        }
        if (IP.find('.') != string::npos) {
            return JudgeIpv4(IP);
        }
        return JudgeIpv6(IP);
    }
    string JudgeIpv4(string& ip) {
        int index = 0, count;
        int time = 0;
        while (index < ip.size()){
            count = 0;
            bool first_time = true;
            while (index < ip.size() && ip[index] != '.') {
                if (ip[index] < '0' || ip[index] > '9') {
                    return "Neither";
                }
                if (first_time && ip[index] == '0' && !IsSingle(ip, index)) {
                    return "Neither";
                }
                first_time = false;
                count = count * 10 + (ip[index] - '0');
                if (count > 255) {
                    return "Neither";
                }
                index += 1;    
            } 
            if (index == ip.size() - 1 && ip[index] == '.') {
                return "Neither";
            }           
            index += 1;
            if (index < ip.size() && !(ip[index] >= '0' && ip[index] <= '9')) {
                return "Neither";
            }
            time += 1;
        }
        if (time != 4) {
            return "Neither";
        }
        return "IPv4";
    }
    bool IsSingle(string& ip, int index) {
        if (ip[index] == '0') {
            if (index + 1 == ip.size() || ip[index + 1] == '.') {
                return true;
            }
        }
        return false;
    }
    string JudgeIpv6(string& ip) {
        int index = 0, count;
        int time = 0;
        while (index < ip.size()) {
            count = 0;
            while (index < ip.size() && ip[index] != ':') {
                if (!IsAlNum(ip[index])) {
                    return "Neither";
                }
                count += 1;
                index += 1;
            }
            
            if (index + 1 < ip.size() && !IsAlNum(ip[index + 1])) {
                return "Neither";
            }
            if (count > 4) {
                return "Neither";
            }
            if (index == ip.size() - 1 && ip[index] == ':') {
                return "Neither";
            }
            index += 1;
            if (index < ip.size() && !IsAlNum(ip[index])) {
                return "Neither";
            }
            time += 1;
        }
        if (time != 8) {
            return "Neither";
        }
        return "IPv6";
    }
    bool IsAlNum(char ch) {
        return ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'f' ||
                ch >= 'A' && ch <= 'F';
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值