-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restore IP Addresses.cpp
73 lines (69 loc) · 1.97 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
private:
vector<string> ret;
int atoi(string s){
int ret = 0;
for (int i=0; i<s.length(); i++) {
ret = ret * 10 + s[i] - '0';
}
return ret;
}
void output(const string & s, vector<int>pos) {
string cret = s.substr(0, pos[0]);
cret += ".";
cret += s.substr(pos[0], pos[1]-pos[0]);
cret += ".";
cret += s.substr(pos[1], pos[2]-pos[1]);
cret += ".";
cret += s.substr(pos[2], s.length()-pos[2]);
ret.push_back(cret);
}
void recurive(const string &s, vector<int>pos) {
if (pos.size()==3) {
if (pos[2]>=s.length()) {
return;
}
string x = s.substr(pos[2], s.length()-pos[2]);
if (x.length()>1 && x[0]=='0') {
return;
}
int num = atoi(x);
if (num>=0 && num<=255) {
output(s, pos);
}
}
for (int i=1; i<4; i++) {
int begin=0;
if (pos.size()>0) {
begin = pos[pos.size()-1];
}
if (begin>=s.length()) {
return;
}
if (i>1 && s[begin]=='0'){
return;
}
string x = s.substr(begin, i);
int num = atoi(x);
pos.push_back(0);
if (num>=0 && num<=255) {
pos[pos.size()-1] = begin+i;
recurive(s, pos);
}
pos.pop_back();
}
}
public:
vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
vector<int> pos;
pos.clear();
if (s.length()>12){
return ret;
}
recurive(s, pos);
return ret;
}
};