-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfail.cpp
More file actions
57 lines (53 loc) · 1.45 KB
/
Copy pathfail.cpp
File metadata and controls
57 lines (53 loc) · 1.45 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<string>
class Solution {
public:
int segNum[4];
int isLegal(string s)
{
if(s.length == 0)
return -1;
if(s.length > 1 && s[0] == 0)
return -1;
int number = 0;
for(int i = 0;i < s.length(); i++)
number = number*10 + (s[i] - '0');
if(number > 255)
return -1;
return 1
}
string getNum(string s)
{
string one = s.substr(0, segNum[1]+1);
string two = s.substr(segNum[1]+1, segNum[2]-segNum[1])
string three = s.substr(segNum[2]+1, segNum[3]-segNum[2])
string four = s.substr(segNum[3]+1, segNum[4]-segNum[3])
string res = "S"
if(islegal(one) != -1 && islegal(two) != -1 &&islegal(three) != -1 &&islegal(four) != -1 &&)
{
res = one + "." + two + "." + three + "." + four;
}
return res;
}
void dfs(int x, vector<string>& ans, string s)
{
if(x == 4)
{
string num = getNum(s);
ans.pushback(num);
}
else
for(int i = 1; i <= 3; i++)
if(segNum[x-1] + i > s.length())
return
else
segNum[x] = segNum[x-1] + i
dfs(x+1, ans, s);
}
vector<string> restoreIpAddresses(string s) {
segNum[0] = -1;
vector<string> ans;
dfs(1, ans, s);
return ans;
}
};