mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加93.复原IP地址JavaScript·版本
This commit is contained in:
@ -338,6 +338,35 @@ class Solution(object):
|
|||||||
return ans```
|
return ans```
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JavaScript:
|
||||||
|
|
||||||
|
```js
|
||||||
|
/**
|
||||||
|
* @param {string} s
|
||||||
|
* @return {string[]}
|
||||||
|
*/
|
||||||
|
var restoreIpAddresses = function(s) {
|
||||||
|
const res = [], path = [];
|
||||||
|
backtracking(0, 0)
|
||||||
|
return res;
|
||||||
|
function backtracking(i) {
|
||||||
|
const len = path.length;
|
||||||
|
if(len > 4) return;
|
||||||
|
if(len === 4 && i === s.length) {
|
||||||
|
res.push(path.join("."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(let j = i; j < s.length; j++) {
|
||||||
|
const str = s.substr(i, j - i + 1);
|
||||||
|
if(str.length > 3 || +str > 255) break;
|
||||||
|
if(str.length > 1 && str[0] === "0") break;
|
||||||
|
path.push(str);
|
||||||
|
backtracking(j + 1);
|
||||||
|
path.pop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user