GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/093_Restore_IP_Addresses.py at master · jimx33/leetcode

Expand file tree

/

093_Restore_IP_Addresses.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
71 lines (66 loc) · 2.14 KB
/

093_Restore_IP_Addresses.py

Copy path

File metadata and controls

71 lines (66 loc) · 2.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Solution(object):
# def restoreIpAddresses(self, s):
# """
# :type s: str
# :rtype: List[str]
# """
# ls = len(s)
# if ls == 0 or ls > 12:
# return []
# if s == '0000':
# return ['0.0.0.0']
# return self.getremainIP(s, 0, 4)
#
# def getremainIP(self, s, pos, count):
# if count == 1:
# curr = s[pos:]
# if self.isNum(curr):
# return [curr]
# result = []
# for i in range(pos, pos + 3):
# if i + 1 < len(s):
# prefix = s[pos:i + 1]
# if self.isNum(prefix):
# res = self.getremainIP(s, i + 1, count - 1)
# result.extend([prefix + '.' + t for t in res])
# return result
#
# def isNum(self, prefix):
# curr = int(prefix)
# if curr > 255:
# return False
# if len(prefix) == 1:
# return True
# if prefix[0] == '0':
# return False
# return True
def restoreIpAddresses(self, s):
ls = len(s)
if ls == 0 or ls > 12:
return []
res = []
for i in range(1, 4):
for j in range(1, 4):
for k in range(1, 4):
m = ls - i - j - k
if m > 0 and m <= 3:
add1 = s[0:i]
add2 = s[i:i + j]
add3 = s[i + j:i + j + k]
add4 = s[i + j + k:]
if self.isValid(add1) and self.isValid(add2) and \
self.isValid(add3) and self.isValid(add4):
res.append(add1 + '.' + add2 + '.' + add3 + '.' + add4)
return res
def isValid(self, add):
if len(add) == 1:
return True
if add[0] == '0':
return False
if int(add) <= 255:
return True
return False
if __name__ == '__main__':
s = Solution()
# print s.longestValidParentheses(")(((((()())()()))()(()))(")
print s.restoreIpAddresses('25525511135')
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.