Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created January 6, 2020 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/8c7e831c5468b6b48e5334890e3613fb to your computer and use it in GitHub Desktop.
Save Desolve/8c7e831c5468b6b48e5334890e3613fb to your computer and use it in GitHub Desktop.
0093 Restore IP Addresses
class Solution:
def checkIP(self, s: str, res: List[str], i: int, cnt: int, cur: str) -> None:
if len(s) - i < 4 - cnt: return
if cnt == 3:
if len(s) - i <= 3:
if s[i] != "0" and int(s[i:]) <= 255 or s[i:] == "0":
cur += s[i:]
res.append(cur)
else:
j = 1
while j <= 3 and i + j <= len(s):
if s[i] != "0" and int(s[i:i+j]) <= 255 or s[i:i+j] == "0":
self.checkIP(s, res, i + j, cnt + 1, cur + s[i: i + j] + ".")
j += 1
def restoreIpAddresses(self, s: str) -> List[str]:
res = []
self.checkIP(s, res, 0, 0, "")
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment