-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path426.h
More file actions
42 lines (39 loc) · 1.09 KB
/
426.h
File metadata and controls
42 lines (39 loc) · 1.09 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
class Solution {
public:
/**
* @param s: the IP string
* @return: All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string &s) {
// write your code here
vector<string> v;
vector<string> r;
IpAddress(s, v, r, 0);
return r;
}
void IpAddress(string &s, vector<string> &v, vector<string> &r, int start){
if(start >= s.size()){
ostringstream oss;
if(v.size() == 4){
for(int j = 0; j < 4; j++){
if(j == 0) oss << v[j];
else oss << '.' << v[j];
}
r.push_back(oss.str());
}
return;
}
for(int i = start; i < start +3 && i < s.size(); i++){
string ip = s.substr(start, i - start + 1);
int num = stoi(ip);
if(num <= MAXIP){
v.push_back(ip);
IpAddress(s, v, r, i + 1);
v.pop_back();
}
if(num == 0) break;
}
}
private:
const int MAXIP = 255;
};