mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
更新0093.复原IP地址python3版本,添加剪枝操作
This commit is contained in:
@ -320,14 +320,15 @@ class Solution:
|
||||
if p[0] == '0': return False
|
||||
if int(p) > 0 and int(p) <256: return True
|
||||
return False
|
||||
|
||||
|
||||
def backtrack(s, startIndex):
|
||||
if len(s) > 12: return # 字符串长度最大为12
|
||||
if len(path) == 4 and startIndex == len(s): # 确保切割完,且切割后的长度为4
|
||||
res.append(".".join(path[:])) # 字符拼接
|
||||
return
|
||||
return
|
||||
|
||||
for i in range(startIndex, len(s)):
|
||||
if len(s) - startIndex > 3*(4 - len(path)): continue # 剪枝,剩下的字符串大于允许的最大长度则跳过
|
||||
p = s[startIndex:i+1] # 分割字符
|
||||
if isValid(p): # 判断字符是否有效
|
||||
path.append(p)
|
||||
|
Reference in New Issue
Block a user