forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0093.py
More file actions
24 lines (22 loc) · 768 Bytes
/
0093.py
File metadata and controls
24 lines (22 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
ans = []
self.dfs(s, 0, 0, [""] * 4, ans)
return ans
def dfs(self, s: str, depth: int, index: int, path: List[str], ans: List[str]) -> None:
if depth == 4 and index == len(s):
ans.append(".".join(path))
return
if depth == 4 or index == len(s):
return
for i in range(1, 4):
if index + i > len(s):
return
if i > 1 and s[index] == '0':
return
temp = s[index: index + i]
if int(temp) > 255:
return
path[depth] = temp
self.dfs(s, depth + 1, index + i, path, ans)
path[depth] = ""