题解 | 数字字符串转化成IP地址
数字字符串转化成IP地址
https://www.nowcoder.com/practice/ce73540d47374dbe85b3125f57727e1e
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @return string字符串一维数组
#
class Solution:
def restoreIpAddresses(self , s: str) -> List[str]:
# write code here
if len(s)>12:
return []
res, path = [], []
def dfs(index):
if index > len(s):
return
if index == len(s) and len(path) == 4:
res.append(".".join(path[:]))
return
for i in range(index, len(s)):
if 0 <= int(s[index:i+1]) <=255:
if s[index] == "0" and len(s[index:i+1]) > 1:
continue
path.append(s[index:i+1])
dfs(i+1)
path.pop()
dfs(0)
return res
