Skip to content

Commit e45d586

Browse files
committedMay 11, 2017
add: Restore IP Addresses
1 parent bfe1eb6 commit e45d586

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
3333
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js)|Medium|
3434
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
3535
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
36-
3736
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/) | [JavaScript](./src/simplify-path/res.js)|Medium|
3837
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [JavaScript](./src/set-matrix-zeroes/res.js)|Medium|
3938
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/) | [JavaScript](./src/sort-colors/res.js)|Medium|
4039
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/) | [JavaScript](./src/decode-ways/res.js)|Medium|
40+
|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) | [JavaScript](./src/restore-ip-addresses/res.js)|Medium|
4141
|120|[Triangle](https://leetcode.com/problems/triangle/) | [JavaScript](./src/triangle/res.js)|Medium|
4242
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js)|Medium|
4343
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/) | [JavaScript](./src/clone-graph/res.js)|Medium|

‎src/restore-ip-addresses/res.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-05-11 10:39:29
5+
*
6+
* @param {string} s
7+
* @return {string[]}
8+
*/
9+
let restoreIpAddresses = function(s) {
10+
let len = s.length,
11+
res = [];
12+
if (len < 4) return [];
13+
14+
for (let first = 1; first < 5; first++) {
15+
if (first === len) break;
16+
for (let second = 1; second < 5; second++) {
17+
if (first + second === len) break;
18+
for (let third = 1; third < 5; third++) {
19+
if (first + second + third === len) break;
20+
let tmp1 = s.substring(0, first),
21+
tmp2 = s.substring(first, first + second),
22+
tmp3 = s.substring(first + second, first + second + third),
23+
tmp4 = s.substring(first + second + third);
24+
25+
if (isValid(tmp1) && isValid(tmp2) && isValid(tmp3) && isValid(tmp4)) {
26+
res.push(`${tmp1}.${tmp2}.${tmp3}.${tmp4}`);
27+
}
28+
}
29+
}
30+
}
31+
32+
return res;
33+
34+
function isValid(str) {
35+
if (str.charAt(0) === '0' && str.length > 1) return false;
36+
37+
let num = Number.parseInt(str);
38+
if (num < 256)
39+
return true;
40+
else
41+
return false;
42+
}
43+
};

0 commit comments

Comments
 (0)
Please sign in to comment.