-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
39 lines (31 loc) · 853 Bytes
/
index.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
/**
* @param {string} s
* @return {string[]}
*/
var restoreIpAddresses = function(s) {
let ans = [];
let n = s.length;
getItem(0, []);
function getItem(len, arr){
if(arr.length === 4 ){
if(len === n){
ans.push(arr);
}
return
}
if(len >= n ) return;
if(s.slice(len,len+1) === '0'){
getItem(len+1, arr.concat(s.slice(len,len+1)));
}else{
getItem(len+1, arr.concat(s.slice(len,len+1)));
getItem(len+2, arr.concat(s.slice(len,len+2)));
if( ~~s.slice(len,len+3) <= 255){
getItem(len+3, arr.concat(s.slice(len,len+3)));
}
}
}
ans = ans.map(function(item){
return item.join('.')
})
return ans;
};