class : defdfs(self, string, part, count): re = ".".join(part) if count == 4andnot string and re notin self.results: self.results.append(re) for j in range(len(string)): if count <= 3and string[:j+1] and int(string[:j+1]) <= 255 # string[:j+1]长度为1 或 string[:j+1]长度大于1但不能以0开头 and (j+1 < 2or (j+1 >= 2and string[0] != '0')): self.dfs(string[j+1:], part + [string[:j+1]], count + 1)
# for i in range(1, 4): # if i <= len(string): # if i == 1: # self.dfs(string[i:], part + [string[:i]], count + 1) # if i==2 and string[0] != '0': # self.dfs(string[i:], part + [string[:i]], count + 1) # if i==3 and string[0] != '0' and int(string[:3]) <= 255 : # self.dfs(string[i:], part + [string[:i]], count + 1) defrestoreIpAddresses(self, s: str) -> List[str]: ifnot s or len(s) > 12: return [] self.results = [] self.dfs(s, [], 0) return self.results
近期评论