-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestoreIPAddress.py
More file actions
31 lines (27 loc) · 857 Bytes
/
RestoreIPAddress.py
File metadata and controls
31 lines (27 loc) · 857 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
25
26
27
28
29
30
31
class Solution:
def restoreIpAddresses(self, s):
return self.traverse(s, 3)
def traverse(self, s, points):
if points == 0:
if self.isValid(s):
return [s]
else:
return [None]
ans = []
for i in range(3):
if self.isValid(s[-i - 1:]):
resl = self.traverse(s[:-i - 1], points - 1)
for val in resl:
if val is not None:
ans.append(val + "." + s[-i - 1:])
return ans
def isValid(self, s):
res = True
res = res and s is not None and s != ""
if len(s) > 1:
res = res and s[0] != '0'
res = res and 0 <= int(s) <= 255
return res
if __name__ == '__main__':
s = Solution()
print(s.restoreIpAddresses("010010"))