-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
37 lines (35 loc) · 976 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ans;
vector<string> t;
dfs(s, t, ans);
return ans;
}
void dfs(string s, vector<string>& t, vector<string>& ans) {
if (t.size() == 4) {
if (s == "") {
string p = "";
for (auto e : t) p += e + ".";
p.pop_back();
ans.push_back(p);
}
return;
}
for (int i = 1; i < min(4, (int) s.size() + 1); ++i) {
string c = s.substr(0, i);
if (check(c)) {
t.push_back(c);
dfs(s.substr(i, s.size() - i), t, ans);
t.pop_back();
}
}
}
bool check(string s) {
if (s == "") return false;
int num = stoi(s);
if (num > 255) return false;
if (s[0] == '0' && s.size() > 1) return false;
return true;
}
};