forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0093.cpp
More file actions
42 lines (31 loc) · 885 Bytes
/
0093.cpp
File metadata and controls
42 lines (31 loc) · 885 Bytes
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:
void dfs(string& s, int start, int step, string ip, vector<string>& result) {
int siz = s.size();
if(start == siz && step == 4) {
ip.erase(ip.end() - 1);
result.push_back(ip);
return;
}
if(siz - start > (4 - step) * 3)
return;
if(siz - start < (4 - step))
return;
int num = 0;
for(int i = start; i < start + 3; i++) {
num = num * 10 + (s[i] - '0');
if(num <= 255) {
ip += s[i];
dfs(s, i + 1, step + 1, ip + '.', result);
}
if(num == 0)
break;
}
}
vector<string> restoreIpAddresses(string s) {
vector<string> result;
string ip;
dfs(s, 0, 0, ip, result);
return result;
}
};