题目:
题解:
class Solution:def validIPAddress(self, queryIP: str) -> str:if queryIP.find(".") != -1:# IPv4last = -1for 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 = 0for j in range(last + 1, cur):if not queryIP[j].isdigit():return "Neither"addr = addr * 10 + int(queryIP[j])if addr > 255:return "Neither"if addr > 0 and queryIP[last + 1] == "0":return "Neither"if addr == 0 and cur - last - 1 > 1:return "Neither"last = curreturn "IPv4"else:# IPv6last = -1for 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 = curreturn "IPv6"