diff --git a/include/s0093_restore_ip_addresses.hpp b/include/s0093_restore_ip_addresses.hpp new file mode 100644 index 0000000..a513da4 --- /dev/null +++ b/include/s0093_restore_ip_addresses.hpp @@ -0,0 +1,14 @@ +#ifndef S0093_RESTORE_IP_ADDRESSES_HPP +#define S0093_RESTORE_IP_ADDRESSES_HPP + +#include +#include + +using namespace std; + +class S0093 { + public: + vector restoreIpAddresses(string s); +}; + +#endif diff --git a/notes/src/backtrack.md b/notes/src/backtrack.md index e673b26..abb238b 100644 --- a/notes/src/backtrack.md +++ b/notes/src/backtrack.md @@ -28,10 +28,15 @@ void backtrack(NodeState &node, vector &result, int para1, int para2, return; } + // 剪枝 + // 当现在的节点不可能出现我们想要的结果时,直接跳过。 + if (/* out of scope */) { + return; + } + // 遍历该节点的所有子节点,即遍历下一层 for (...) { - // 剪枝 - // 当现在的节点不可能出现我们想要的结果时,直接跳过。 + // 剪枝也可以在 for 循环中完成 if (/* out of scope */) { continue; } diff --git a/notes/src/split.md b/notes/src/split.md index 713ddae..bd6a400 100644 --- a/notes/src/split.md +++ b/notes/src/split.md @@ -3,3 +3,5 @@ ## [131. 分割回文串](https://leetcode.cn/problems/palindrome-partitioning/) ![](https://paste.sainnhe.dev/FHTE.jpg) + +## [93. 复原 IP 地址](https://leetcode.cn/problems/restore-ip-addresses/) diff --git a/src/s0093_restore_ip_addresses.cpp b/src/s0093_restore_ip_addresses.cpp new file mode 100644 index 0000000..b42fe44 --- /dev/null +++ b/src/s0093_restore_ip_addresses.cpp @@ -0,0 +1,55 @@ +#include "s0093_restore_ip_addresses.hpp" + +bool isValidIpAddressSegment(const string &s, int begin, int end) { + if (begin > end) { + return false; + } + if (s[begin] == '0' && begin != end) { // 0开头的数字不合法 + return false; + } + int num = 0; + for (int i = begin; i <= end; i++) { + if (s[i] > '9' || s[i] < '0') { // 遇到非数字字符不合法 + return false; + } + num = num * 10 + (s[i] - '0'); + if (num > 255) { // 如果大于255了不合法 + return false; + } + } + return true; +} + +void restoreIpAddressesDFS(vector &dots, vector &result, string &s, + int startIndex) { + // 结束条件 + if (dots.size() == 4 && startIndex == s.length()) { + string ip = s.substr(0, dots[0] + 1) + "." + + s.substr(dots[0] + 1, dots[1] - dots[0]) + "." + + s.substr(dots[1] + 1, dots[2] - dots[1]) + "." + + s.substr(dots[2] + 1, dots[3] - dots[2]); + result.push_back(ip); + } + + // 剪枝 + // 如果已经分成了四段,就没必要再分了 + if (dots.size() == 4) { + return; + } + + // 遍历当前节点的下一层 + for (int i = startIndex; i < startIndex + 3; ++i) { + if (isValidIpAddressSegment(s, startIndex, i)) { + dots.push_back(i); + restoreIpAddressesDFS(dots, result, s, i + 1); + dots.pop_back(); + } + } +} + +vector S0093::restoreIpAddresses(string s) { + vector result{}; + vector dots{}; + restoreIpAddressesDFS(dots, result, s, 0); + return result; +}