-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0093.restore_ip_addresses_M.cpp
66 lines (65 loc) · 1.66 KB
/
0093.restore_ip_addresses_M.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* https://leetcode.com/problems/restore-ip-addresses/description/
* 2015/07
* 4 ms
*/
class Solution
{
private:
string addr;
vector<string> results;
int len;
void segAddr(string ip, int idx, int segs)
{
if(segs == 0)
{
if(idx == len)
results.push_back(ip);
return;
}
if(idx >= len)
return;
if(idx < len && isdigit(addr[idx]))
{
string new_ip = ip;
new_ip += addr[idx];
new_ip += '.';
segAddr(new_ip, idx + 1, segs - 1);
}
if(idx + 1 < len && isdigit(addr[idx + 1]))
{
if(addr[idx] == '0')
return;
string new_ip = ip;
new_ip += addr[idx];
new_ip += addr[idx + 1];
new_ip += '.';
segAddr(new_ip, idx + 2, segs - 1);
}
if(idx + 2 < len && isdigit(addr[idx + 2]))
{
if(addr[idx] == '0')
return;
if((addr[idx] - '0') * 100 + (addr[idx + 1] - '0') * 10 + addr[idx + 2] - '0' > 255)
return;
string new_ip = ip;
new_ip += addr[idx];
new_ip += addr[idx + 1];
new_ip += addr[idx + 2];
new_ip += '.';
segAddr(new_ip, idx + 3, segs - 1);
}
}
public:
vector<string> restoreIpAddresses(string s)
{
addr = s;
len = addr.length();
string ip;
results.clear();
segAddr(ip, 0, 4);
for(int i = 0; i < results.size(); i ++)
results[i].erase(results[i].length() - 1, 1);
return results;
}
};