-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path93+Restore IP Addresses.cpp
More file actions
46 lines (34 loc) · 1.04 KB
/
93+Restore IP Addresses.cpp
File metadata and controls
46 lines (34 loc) · 1.04 KB
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
44
45
46
class Solution {
public:
vector<string> res;
bool isValid(string s) {
if (s.size() == 1) return true;
if (s[0] - '0' == 0 || s.size() > 3) return false;
long long num = 0;
for (int i = 0; i < s.size(); ++i) {
num = num*10L + s[i] - '0';
}
return num > 255 ? false : true;
}
void dfs(string &s, int start, string sT, int cnt) {
if (cnt > 4) {
return;
}
if (start == s.size() && cnt == 4) {
sT = sT.substr(0, sT.size()-1);
res.push_back(sT);
}
for (int end = start; end <= start+3 && end < s.size(); ++end) {
if (isValid(s.substr(start, end-start+1))) {
string tmp = s.substr(start, end-start+1);
dfs(s, end+1, sT + tmp + '.', cnt+1);
}
}
return;
}
vector<string> restoreIpAddresses(string s) {
string sT;
dfs(s, 0, sT, 0);
return res;
}
};