题解 | #数字字符串转化成IP地址#
数字字符串转化成IP地址
https://www.nowcoder.com/practice/ce73540d47374dbe85b3125f57727e1e
class Solution:
def restoreIpAddresses(self , s: str) -> List[str]:
if len(s) < 4 or len(s) > 12:
return None
res = []
path = []
def backrack(idx):
if len(path) == 4 and idx == len(s):
res.append('.'.join(path))
return
for i in range(1, 4):
if idx+i > len(s):
continue
sub = s[idx:idx+i]
if len(sub) > 1 and sub[0] == '0':
continue
if int(sub) > 255:
continue
path.append(sub)
backrack(idx+i)
path.pop()
backrack(0)
return res
查看11道真题和解析