力扣每日一题2022-05-29中等题:验证IP地址

验证IP地址

    • 题目描述
    • 思路
      • 模拟
        • Python实现
        • Java实现
        • C++实现


题目描述

验证IP地址


思路

模拟

根据题意模拟即可。
观察到有效的IPv4地址中均存在’.‘符号,IPv4有4个部分用’.‘隔开。如果存在’.',则考虑是否为IPv4地址,可以分割字符串,对于分割的每一部分,考虑是否满足以下条件,如果满足,则返回IPv4,如果不满足则返回neither:

  • 长度是否在[1, 3]之间;
  • 是否为纯数字;
  • 值是否在[0, 255]之间;
  • 是否不包含前导零。如果值为0,则该部分长度只能为1;如果值不为0,则该部分的第一个数字不能为0。

对于IPv6地址而言,均以’:‘分隔,共包含8个部分。如果存在’:',则同样对地址进行分割,对每个部分进行判断,如果满足条件则返回IPv6,如果不满足则返回Neither。条件如下:

  • 长度在[1, 4]之间;
  • 是否只包含数字,或者a-f,或者A-F。

除了上述两种情况,如果无法找到对应数量的部分,给定的字符串也不是一个有效的IP地址。

Python实现

class Solution:
    def validIPAddress(self, queryIP: str) -> str:
        if queryIP.find('.') != -1:
            # 可能是IPv4
            last = -1
            for i in range(4):
                cur = (len(queryIP) if i == 3 else queryIP.find('.', last+1))
                if cur == -1:
                    return "Neither"
                if not 1 <= cur-last-1 <= 3:
                    return "Neither"
                addr = 0
                for j in range(last+1, cur):
                    if not queryIP[j].isdigit():
                        return "Neither"
                    addr = addr * 10 + int(queryIP[j])
                if addr > 255 or (addr > 0 and queryIP[last+1] == '0') or (addr == 0 and cur-last-1>1):
                    return "Neither"
                last = cur
            return "IPv4"
        else:
            # 可能是IPv6
            last = -1
            for i in range(8):
                cur = (len(queryIP) if i == 7 else queryIP.find(":", last+1))
                if cur == -1:
                    return "Neither"
                if not 1 <= cur-last-1 <= 4:
                    return "Neither"
                for j in range(last+1, cur):
                    if not queryIP[j].isdigit() and not ("a" <= queryIP[j].lower() <= "f"):
                        return "Neither"
                last = cur
            return "IPv6"

Java实现

class Solution {
    public String validIPAddress(String queryIP) {
        if (queryIP.indexOf('.') >= 0) {
            // IPv4
            int last = -1;
            for (int i = 0; i < 4; ++i) {
                int cur = (i == 3 ? queryIP.length() : queryIP.indexOf('.', last + 1));
                if (cur < 0) {
                    return "Neither";
                }
                if (cur - last - 1 < 1 || cur - last - 1 > 3) {
                    return "Neither";
                }
                int addr = 0;
                for (int j = last + 1; j < cur; ++j) {
                    if (!Character.isDigit(queryIP.charAt(j))) {
                        return "Neither";
                    }
                    addr = addr * 10 + (queryIP.charAt(j) - '0');
                }
                if (addr > 255) {
                    return "Neither";
                }
                if (addr > 0 && queryIP.charAt(last + 1) == '0') {
                    return "Neither";
                }
                if (addr == 0 && cur - last - 1 > 1) {
                    return "Neither";
                }
                last = cur;
            }
            return "IPv4";
        } else {
            // IPv6
            int last = -1;
            for (int i = 0; i < 8; ++i) {
                int cur = (i == 7 ? queryIP.length() : queryIP.indexOf(':', last + 1));
                if (cur < 0) {
                    return "Neither";
                }
                if (cur - last - 1 < 1 || cur - last - 1 > 4) {
                    return "Neither";
                }
                for (int j = last + 1; j < cur; ++j) {
                    if (!Character.isDigit(queryIP.charAt(j)) && !('a' <= Character.toLowerCase(queryIP.charAt(j)) && Character.toLowerCase(queryIP.charAt(j)) <= 'f')) {
                        return "Neither";
                    }
                }
                last = cur;
            }
            return "IPv6";
        }
    }
}

C++实现

class Solution {
public:
    string validIPAddress(string queryIP) {
        if (queryIP.find('.') != string::npos) {
            // IPv4
            int last = -1;
            for (int i = 0; i < 4; ++i) {
                int cur = (i == 3 ? queryIP.size() : queryIP.find('.', last + 1));
                if (cur == string::npos) {
                    return "Neither";
                }
                if (cur - last - 1 < 1 || cur - last - 1 > 3) {
                    return "Neither";
                }
                int addr = 0;
                for (int j = last + 1; j < cur; ++j) {
                    if (!isdigit(queryIP[j])) {
                        return "Neither";
                    }
                    addr = addr * 10 + (queryIP[j] - '0');
                }
                if (addr > 255) {
                    return "Neither";
                }
                if (addr > 0 && queryIP[last + 1] == '0') {
                    return "Neither";
                }
                if (addr == 0 && cur - last - 1 > 1) {
                    return "Neither";
                }
                last = cur;
            }
            return "IPv4";
        }
        else {
            // IPv6
            int last = -1;
            for (int i = 0; i < 8; ++i) {
                int cur = (i == 7 ? queryIP.size() : queryIP.find(':', last + 1));
                if (cur == string::npos) {
                    return "Neither";
                }
                if (cur - last - 1 < 1 || cur - last - 1 > 4) {
                    return "Neither";
                }
                for (int j = last + 1; j < cur; ++j) {
                    if (!isdigit(queryIP[j]) && !('a' <= tolower(queryIP[j]) && tolower(queryIP[j]) <= 'f')) {
                        return "Neither";
                    }
                }
                last = cur;
            }
            return "IPv6";
        }
    }
};

你可能感兴趣的:(leetcode每日一题,leetcode,python,java,c++)