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
|
class Solution(object):
def restoreIpAddresses(self, s):
if (len(s)<4) or (len(s)>12):
return []
else:
output=[]
def get_output(list_temp):
def judge(string_temp):
if ((string_temp[0]=="0") and(len(string_temp)>1)) or (int(string_temp)>255):
return False
else:
return True
for item in list_temp:
cnt=0
s1=s[cnt: cnt+int(str(item)[0])]
if !judge(s1):
continue
cnt+=int(str(item)[0])
s2=s[cnt: cnt+int(str(item)[1])]
if !judge(s2):
continue
cnt+=int(str(item)[1])
s3=s[cnt: cnt+int(str(item)[2])]
if !judge(s3):
continue
cnt+=int(str(item)[2])
s4=s[cnt:cnt+int(str(item)[3])]
if !judge(s4):
continue
ss= s1+"."+s2+"."+s3+"."+s4
if ss in output:
continue
else:
output.append(ss)
return output
if (len(s)-4==0):
return [s[0]+"."+s[1]+"."+s[2]+"."+s[3]]
elif (len(s)-4==1):
return get_output([2111,1211,1121,1112])
elif (len(s)-4==2):
return get_output([1122,1212,1221,2112,2121,2211,3111,1311,1131,1113])
elif (len(s)-4==3):
return get_output([2221,2212,2122,1222,3211,3121,3112,2311,2131,2113,1321,1312,1231,1213,1132,1123])
elif (len(s)-4==4):
return get_output([2222,3221,3212,3122,2321,2312,2231,2213,2132,2123,1322,1232,1223,3311,3131,3113,1331,1313,1133])
elif (len(s)-4==5):
return get_output([3222,2322,2232,2223,3321,3312,3231,3213,3132,3123,2331,2313,2133,1332,1323,1233])
elif (len(s)-4==6):
# list_t=[3322,3232,3223,2332,2323,2233,3331,3313,3133,1333]
print(6)
return get_output([3322,3232,3223,2332,2323,2233,3331,3313,3133,1333])
elif (len(s)-4==7):
# list_t=[3332,3323,3233,2333]
print(7)
return get_output([3332,3323,3233,2333])
elif (len(s)-4==8):
# list_t=[3333]
print(8)
return get_output([3333])
|