-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestore_IP_Addresses.cpp
43 lines (37 loc) · 1.24 KB
/
Restore_IP_Addresses.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
38
39
40
41
42
43
class Solution {
public:
bool digitValid(string s) {
if (s.size() > 1 && s[0] == '0')
return false;
int num = 0;
for (int i = 0; i < s.size(); ++i)
num = num * 10 + (s[i] - '0');
if(num < 0 || num > 255)
return false;
return true;
}
void restoreIpAddressesRecursion(string &s, int pos, vector<string> &tmp, vector<string> &v) {
if(tmp.size() == 4 && pos == s.size()) {
v.push_back(tmp[0] + '.' + tmp[1] + '.' + tmp[2] + '.' + tmp[3]);
return;
}
if(tmp.size() < 4) {
for (int len = 1; pos + len - 1 < s.size() && len <= 3; ++len) {
string newPart = s.substr(pos, len);
if (digitValid(newPart)) {
tmp.push_back(newPart);
restoreIpAddressesRecursion(s, pos+len, tmp, v);
tmp.pop_back();
}
}
}
}
vector<string> restoreIpAddresses(string s) {
vector<string> v;
if (s.size() < 4 || s.size() > 12)
return v;
vector<string> tmp;
restoreIpAddressesRecursion(s, 0, tmp, v);
return v;
}
};