-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugDFS.cpp
77 lines (68 loc) · 1.59 KB
/
debugDFS.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
74
75
76
77
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
bool isValid(const string &s)
{
int num = stoi(s);
return num >= 0 && num <= 255;
}
void dfs(string& s, vector<string>& path, vector<string> &ret, int index)
{
int len = s.size();
if (path.size() == 4)
{
if (index == len)
{
stringstream ss;
for (const auto &str : path)
{
ss << str << ".";
}
string sol = ss.str();
sol.pop_back();
ret.emplace_back(sol);
}
return; // find a solution
}
for (int i = index; i < index + 3 && i < len; i++)
{
string sub = s.substr(index, i + 1 - index);
if (s[index] == '0' && i != index)
{
break;
}
if (!isValid(sub))
{
continue;
}
cout << "substring is: " << sub << ", index: " << index << ", i + 1 - index + 1: " << i + 1 - index << endl;
path.emplace_back(sub);
dfs(s, path, ret, i + 1);
path.pop_back();
}
}
/**
* @param s the IP string
* @return All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string& s)
{
// Write your code here
vector<string> ret;
if (s.empty() || s.length() < 4)
{
return ret;
}
vector<string> path;
dfs(s, path, ret, 0);
return ret;
}
int main() {
string s = "0000";
vector<string> result = restoreIpAddresses(s);
for (const auto & element : result) {
cout << element << endl;
}
}