468. Validate IP Address

2016-12-26  本文已影响0人  阿团相信梦想都能实现
class Solution(object):
    def validIPAddress(self, IP):
        """
        :type IP: str
        :rtype: str
        """
        if not IP: return 'Neither'
        valid_char='0123456789abcdefABCDEF'
        array=IP.split('.')
        if len(array)==4:
            for i in range(len(array)):
                if (not array[i].isdigit()) or (len(array[i])>1 and array[i][0]=='0')  or int(array[i])<0 or int(array[i])>255 :
                    return 'Neither'
            return 'IPv4'
        array=IP.split(':')
        if len(array)==8:
            for i in range(len(array)):
                if len(array[i])>4 or len(array[i])==0 or any(c not in valid_char for c in array[i]):
                    return 'Neither'
            return 'IPv6'
        return 'Neither'
        
        
上一篇 下一篇

猜你喜欢

热点阅读