diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 9d4d5918..81192ce5 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -360,104 +360,82 @@ class Solution { ## python -python2: -```python -class Solution(object): - def restoreIpAddresses(self, s): - """ - :type s: str - :rtype: List[str] - """ - ans = [] - path = [] - def backtrack(path, startIndex): - if len(s) > 12: return [] - if len(path) == 4: - if startIndex == len(s): - ans.append(".".join(path[:])) - return - for i in range(startIndex+1, min(startIndex+4, len(s)+1)): # 剪枝 - string = s[startIndex:i] - if not 0 <= int(string) <= 255: - continue - if not string == "0" and not string.lstrip('0') == string: - continue - path.append(string) - backtrack(path, i) - path.pop() - - backtrack([], 0) - return ans -``` - -python3: +回溯(版本一) ```python class Solution: - def __init__(self): - self.result = [] - def restoreIpAddresses(self, s: str) -> List[str]: - ''' - 本质切割问题使用回溯搜索法,本题只能切割三次,所以纵向递归总共四层 - 因为不能重复分割,所以需要start_index来记录下一层递归分割的起始位置 - 添加变量point_num来记录逗号的数量[0,3] - ''' - self.result.clear() - if len(s) > 12: return [] - self.backtracking(s, 0, 0) - return self.result + result = [] + self.backtracking(s, 0, 0, "", result) + return result - def backtracking(self, s: str, start_index: int, point_num: int) -> None: - # Base Case - if point_num == 3: - if self.is_valid(s, start_index, len(s)-1): - self.result.append(s[:]) + def backtracking(self, s, start_index, point_num, current, result): + if point_num == 3: # 逗点数量为3时,分隔结束 + if self.is_valid(s, start_index, len(s) - 1): # 判断第四段子字符串是否合法 + current += s[start_index:] # 添加最后一段子字符串 + result.append(current) return - # 单层递归逻辑 - for i in range(start_index, len(s)): - # [start_index, i]就是被截取的子串 - if self.is_valid(s, start_index, i): - s = s[:i+1] + '.' + s[i+1:] - self.backtracking(s, i+2, point_num+1) # 在填入.后,下一子串起始后移2位 - s = s[:i+1] + s[i+2:] # 回溯 - else: - # 若当前被截取的子串大于255或者大于三位数,直接结束本层循环 - break - - def is_valid(self, s: str, start: int, end: int) -> bool: - if start > end: return False - # 若数字是0开头,不合法 - if s[start] == '0' and start != end: - return False - if not 0 <= int(s[start:end+1]) <= 255: - return False - return True -``` -python3; 简单拼接版本(类似Leetcode131写法): + for i in range(start_index, len(s)): + if self.is_valid(s, start_index, i): # 判断 [start_index, i] 这个区间的子串是否合法 + sub = s[start_index:i + 1] + self.backtracking(s, i + 1, point_num + 1, current + sub + '.', result) + else: + break + + def is_valid(self, s, start, end): + if start > end: + return False + if s[start] == '0' and start != end: # 0开头的数字不合法 + return False + num = 0 + for i in range(start, end + 1): + if not s[i].isdigit(): # 遇到非数字字符不合法 + return False + num = num * 10 + int(s[i]) + if num > 255: # 如果大于255了不合法 + return False + return True + +``` +回溯(版本二) + ```python -class Solution: +class Solution: def restoreIpAddresses(self, s: str) -> List[str]: - global results, path results = [] - path = [] - self.backtracking(s,0) + self.backtracking(s, 0, [], results) return results - def backtracking(self,s,index): - global results,path - if index == len(s) and len(path)==4: - results.append('.'.join(path)) # 在连接时需要中间间隔符号的话就在''中间写上对应的间隔符 + def backtracking(self, s, index, path, results): + if index == len(s) and len(path) == 4: + results.append('.'.join(path)) return - for i in range(index,len(s)): - if len(path)>3: break # 剪枝 - temp = s[index:i+1] - if (int(temp)<256 and int(temp)>0 and temp[0]!='0') or (temp=='0'): - path.append(temp) - self.backtracking(s,i+1) - path.pop() + + if len(path) > 4: # 剪枝 + return + + for i in range(index, min(index + 3, len(s))): + if self.is_valid(s, index, i): + sub = s[index:i+1] + path.append(sub) + self.backtracking(s, i+1, path, results) + path.pop() + + def is_valid(self, s, start, end): + if start > end: + return False + if s[start] == '0' and start != end: # 0开头的数字不合法 + return False + num = int(s[start:end+1]) + return 0 <= num <= 255 + + + + ``` + + ## Go ```go