LeetCode 93. Restore IP Addresses
Solution1:我的答案
怎么就这么慢。。。
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
set<string> res;
vector<string> temp;
my_res(res, temp, s, 0);
return vector<string>(res.begin(), res.end());
}
void my_res(set<string>& res, vector<string>& temp, string& s, int start) {
if (temp.size() == 4 && start == s.size()) {
res.insert(temp[0] + "." + temp[1] + "." + temp[2] + "." + temp[3]);
return;
} else if (temp.size() == 4 && start < s.size()
|| temp.size() < 4 && start == s.size())
return;
else {
for (int i = start; i < s.size(); i++) {
string temp_str = s.substr(start, i - start + 1);
if (temp_str.size() == 1 || (temp_str.size() > 1 && temp_str[0] != '0')
&& atoi(temp_str.c_str()) >= 0 && atoi(temp_str.c_str()) <= 255) {
temp.push_back(temp_str);
my_res(res, temp, s, i + 1);
temp.pop_back();
}
}
}
}
};