-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode093_Restore_IP_Addresses.cpp
More file actions
55 lines (45 loc) · 1.13 KB
/
LeetCode093_Restore_IP_Addresses.cpp
File metadata and controls
55 lines (45 loc) · 1.13 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
47
48
49
50
51
52
53
54
55
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
string tmp;
get_res(tmp, 0, 0, s);
return res;
}
void get_res(string tmp, int pos, int cur, string s)
{
if(pos == 4)
{
if(cur == s.size())
res.push_back(tmp);
return;
}
for(int i = 1; i <= 3 && i+cur<=s.size(); i++)
{
string t = s.substr(cur, i);
int num = atoi(t.c_str());
if(i == 2)
{
if(num/10 == 0)
return;
}
else if(i == 3)
{
if(num/100 == 0)
return;
}
if(num < 256)
{
if(pos == 0)
{
get_res(t, pos+1, i+cur, s);
}
else
{
get_res(tmp+'.'+t, pos+1, i+cur, s);
}
}
}
}
private:
vector<string> res;
};