From 533964af4b31bf1d08f45e626f562616ae12ca8b Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Sat, 5 Jun 2021 18:46:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80JavaScript=C2=B7=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 3c8e5d9d..a8b9a215 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -338,6 +338,35 @@ class Solution(object): 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)