/** * * @param s string字符串 * @return string字符串一维数组 */ function restoreIpAddresses( s ) { // write code here let res = []; let tmp = []; if(s.length < 4) { return res; } dfs(s, res, tmp); return res; } function dfs(s, res, tmp) { if (s.length === 0 && tmp.length === 4) res.push(convert(tmp)); if (tmp.length === 3 && (s > 255)) return; for (let i = 1; i <= s.length; ++i) { if (i > 1 && s[0] == '0') break; let val = s.substr(0, i); if (val > 255) break; if(tmp.length >= 4) break; tmp.push(s.substr(0, i)); dfs(s.substr(i), res, tmp); tmp.pop(); } } function convert(tmp) { return `${tmp[0]}.${tmp[1]}.${tmp[2]}.${tmp[3]}`; } module.exports = { restoreIpAddresses : restoreIpAddresses };