-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path93.zy445566.js
47 lines (47 loc) · 1.36 KB
/
93.zy445566.js
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
/**
* @param {string} s
* @return {string[]}
*/
var restoreIpAddresses = function(s) {
let rightList = [];
function lineStr(s,index,need) {
let endIndex = index+need;
let tmpStr = '';
for (let i=index;i<endIndex;i++) {
if (i>=s.length) {return false;}
tmpStr+=s[i]
}
return tmpStr;
}
function checkAIP(num) {
if (num>255 || (num.length>1 && num[0]=='0'))
{
return true;
}
return false;
}
let cache = {};
for(let i = 1;i<=3;i++) {
for (let j = 1;j<=3;j++) {
for (let k = 1;k<=3;k++) {
for (let l = 1;l<=3;l++) {
if (i+j+k+l!=s.length){continue;}
let a = lineStr(s,0,i);
if (checkAIP(a)) {continue;}
let b = lineStr(s,i,j);
if (checkAIP(b)) {continue;}
let c = lineStr(s,i+j,k);
if (checkAIP(c)) {continue;}
let d = lineStr(s,i+j+k,l);
if (checkAIP(d)) {continue;}
let ip = `${a}.${b}.${c}.${d}`;
if (!cache[ip]) {
cache[ip] = true;
rightList.push(ip)
}
}
}
}
}
return rightList;
};