We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
出处:LeetCode 算法第93题 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。 示例: 输入: "25525511135" 输出: ["255.255.11.135", "255.255.111.35"]
出处:LeetCode 算法第93题
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
示例:
输入: "25525511135" 输出: ["255.255.11.135", "255.255.111.35"]
典型的深度遍历,遍历所有的可能
/** * @param {string} s * @return {string[]} */ function restoreIpAddresses(s) { const res = []; dfs([], 0); return res; function dfs(prefix, idx) { if (prefix.length === 4 && idx === s.length) { res.push(prefix.join('.')); return; } if (prefix.length === 4 || idx === s.length) { return; } for (let r = idx; r < s.length; r++) { if (r !== idx && s[idx] === '0') return; const num = parseInt(s.slice(idx, r + 1)); if (num > 255) { return; } prefix.push(num); dfs(prefix, r + 1); prefix.pop(); } } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
习题
思路
典型的深度遍历,遍历所有的可能
解答
The text was updated successfully, but these errors were encountered: