-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path93.复原-ip-地址.js
More file actions
45 lines (41 loc) · 912 Bytes
/
93.复原-ip-地址.js
File metadata and controls
45 lines (41 loc) · 912 Bytes
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
/*
* @lc app=leetcode.cn id=93 lang=javascript
*
* [93] 复原 IP 地址
*/
// @lc code=start
/**
* @param {string} s
* @return {string[]}
*/
var restoreIpAddresses = function(s) {
if(s.length < 4 || s.length > 12) {
return []
}
const result = new Set()
function fn(start, ip) {
if(start >= s.length || ip.length === 4) {
const ipString = ip.join('.')
if(ipString.length === s.length + 3) {
result.add(ipString)
}
return
}
for(let i = start + 1; i <= start + 3; i++) {
const subStr = s.substring(start, i)
if(+subStr <= 255) {
if(subStr.startsWith('0')) {
if(subStr.length === 1) {
fn(i, [ ...ip, subStr ])
}
}else {
fn(i, [ ...ip, subStr ])
}
}
}
}
fn(0, [])
return Array.from(result)
};
// @lc code=end
// console.log(restoreIpAddresses('101023'))