-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_ip.cpp
More file actions
40 lines (39 loc) · 1.02 KB
/
Copy pathrestore_ip.cpp
File metadata and controls
40 lines (39 loc) · 1.02 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
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
std::vector<string> res;
if(s.length() == 0) return res;
helper(s, 0, 1, res, "");
return res;
}
void helper(string s, int index, int segment, vector<string>& res, string item)
{
if(index >= s.length()) return;
if (segment == 4)
{
string temp_sub = s.substr(index);
if(isValid(temp_sub))
{
res.push_back(item + '.' + temp_sub);
}
return;
}
for(int i = 1; i < 4 && index + i <= s.length(); i++)
{
string temp_sub = s.substr(index, i);
if(isValid(temp_sub))
{
if(segment == 1) helper(s, index + i, segment + 1, res, temp_sub);
else helper(s, index + i, segment + 1, res, item + '.' + temp_sub);
}
}
}
bool isValid(string s)
{
if(s.length() > 3) return false;
int num = atoi(s.c_str());
if(s[0] == '0' && s.length() > 1) return false;
if(num > 255) return false;
return true;
}
};