题目:
代码(首刷看解析 2024年2月3日):
class Solution {
public:vector<string> res;bool Enligible(string& s, int left, int right) {if (left > right) return false;if (s[left] == '0' && left != right) return false;int num = 0;for (int i = left; i <= right; ++i) {if (s[i] - '0' > 9 || s[i] - '0' < 0) return false;num = num * 10 + (s[i] - '0');if (num > 255) return false;}return true;}void backtracking(string& s, int startIndex, int pointNubmber) {if (pointNubmber == 3) {if (Enligible(s, startIndex, s.size() - 1)) res.push_back(s);return;}for (int i = startIndex; i < s.size(); ++i) {if (Enligible(s, startIndex, i)) {s.insert(s.begin() + i + 1, '.');pointNubmber++;backtracking(s, i + 2, pointNubmber);pointNubmber--;s.erase(s.begin() + i + 1);} else {break;}}return;}vector<string> restoreIpAddresses(string s) {if (s.size() < 4) return res;backtracking(s, 0, 0);return res; }
};
哎,自己写的代码错误一堆