当先锋百科网

首页 1 2 3 4 5 6 7

468. 验证IP地址

class Solution:
    def validIPAddress(self, queryIP: str) -> str:
        def isIPv4(ip: str) -> bool:
            return all(s and s.isdigit() and not(s[0] == '0' and len(s) > 1) and 0 <= int(s) <= 255 for s in sp) if len(sp := ip.split(".")) == 4 else False
        
        def isIPv6(ip: str) -> bool:
            return all(s and len(s) <= 4 and all(c in "0123456789ABCDEFabcdef" for c in s) for s in sp) if len(sp := ip.split(":")) == 8 else False

        if "." in queryIP and ":" not in queryIP and isIPv4(queryIP):
            return "IPv4"
        elif ":" in queryIP and "." not in queryIP and isIPv6(queryIP):
            return "IPv6"
        return "Neither"