美文网首页
Leetcode468. 验证IP地址

Leetcode468. 验证IP地址

作者: LonnieQ | 来源:发表于2019-11-09 01:45 被阅读0次

题目

编写一个函数来验证输入的字符串是否是有效的 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 地址。

C++解法

#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;
class Solution {
public:
    string validIPAddress(string IP) {
        bool ipv4 = false;
        bool ipv6 = false;
        int numberOfPart = 0;
        string str;
        for (int i = 0; i < IP.size(); i++) {
            auto c = IP[i];
            if (isalnum(c)) {
                str.push_back(c);
            } else {
                if (!ipv4 && !ipv6) {
                    if (c == ':') ipv6 = true;
                    if (c == '.') ipv4 = true;
                } else if ((c == ':' && ipv4) || (c == '.' && ipv6)) {
                    return "Neither";
                }
                if (i == IP.size() - 1) return "Neither";
                ++numberOfPart;
            }
            if (!isalnum(c) || i == IP.size() - 1) {
                if (str.empty()) return "Neither";
                if (ipv4) {
                    if (str.size() > 3 || (str.size() > 1 && str[0] == '0')) return "Neither";
                    int num = 0;
                    for (auto t: str) {
                        num *= 10;
                        int val = t - '0';
                        if (val < 0 || val > 9) { return "Neither"; }
                        num += val;
                    }
                    if (num < 0 || num > 255) { return "Neither"; }
                } else {
                    if (str.size() > 4) return "Neither";
                    for (auto t: str) {
                        bool valid = (t >= '0' && t <= '9') || (t >= 'A' && t <= 'F') || (t >= 'a' && t <= 'f');
                        if (!valid) return "Neither";
                    }
                }
                str.clear();
            }
        }
        if (ipv4 && numberOfPart == 3) return "IPv4";
        if (ipv6 && numberOfPart == 7) return "IPv6";
        return "Neither";
    }
};
int main(int argc, const char * argv[]) {
    // insert code here...
    Solution solution;
    cout << solution.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334") << endl;
    cout << solution.validIPAddress("256.256.256") << endl;
    cout << solution.validIPAddress("172.16.254.1") << endl;
    cout << solution.validIPAddress("02001:0db8:85a3:0000:0000:8a2e:0370:7334") << endl;
    cout << solution.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334:") << endl;
    
    return 0;
}

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/validate-ip-address

相关文章

网友评论

      本文标题:Leetcode468. 验证IP地址

      本文链接:https://www.haomeiwen.com/subject/agjgbctx.html