题解 | #验证IP地址#

验证IP地址

https://www.nowcoder.com/practice/55fb3c68d08d46119f76ae2df7566880

class Solution:
    def solve(self , IP: str) -> str:
        for ip in IP:
            if not ip in ".:0123456789ABCDEFabcdef":
                return "Neither"

        if(IP.count('.')==3):
            for e in IP.split('.'):
                if not e.isdigit() or int(e)>255 or str(int(e))!=e:
                    return "Neither"
            return "IPv4"

        elif(IP.count(':')==7):
            for e in IP.split(':'):
                if(len(e)>4 or len(e)==0):
                    return "Neither"
            return "IPv6"

        else:
            return "Neither"

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务