468. Validate IP Address
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'
本文标题:468. Validate IP Address
本文链接:https://www.haomeiwen.com/subject/ogjjvttx.html
网友评论