1、之前写回溯都是判断path的长度,而从切割问题开始,判断的是start与s的长度
2、if前面一个条件是去除数值前面带0的,如将023去除,后面一个条件是专门把等于0的拿出,如将00,000的去除
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
def backtracking(path, start):
if len(path) > 4:
return
if start == len(s) and len(path) == 4:
res.append(".".join(path[:]))
else:
for i in range(start, n):
if (0 < int(s[start:i + 1]) <= 255 and not s[start:i + 1].startswith("0")) or (int(s[start:i + 1])==0 and len(s[start:i + 1])==1):
path.append(s[start:i + 1])
backtracking(path, i + 1)
path.pop()
n = len(s)
res = []
backtracking([], 0)
return res