-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path093_Restore_IP_Addresses.cpp
76 lines (75 loc) · 1.51 KB
/
093_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
74
75
76
#include"struct_define.h"
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
static const auto x=[](){
std::ios::sync_with_stdio(false);
std:cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
void helper(int cur)
{
if( cur == 4)
{
string t;
int sum = 0;
for(int i = 0; i < 4; i++)
{
auto tt = s.substr(sum, lens[i]);
if(tt[0]=='0'&&tt.length() > 1) return;
t += tt;
sum += lens[i];
if( i < 3) t+=".";
}
this->res.push_back(t);
return;
}
for(int i = 1; i < 4; i++)
{
lens[cur] = i;
int sum=0;
for(int i = 0; i < cur;i++)
{
sum += lens[i];
}
if( cur == 3 )
{
if( i == (s.length()-sum))
{
if( stoi(s.substr(sum,i)) <= 255)
helper(cur+1);
}
}else
{
if( sum < s.length() && stoi(s.substr(sum,i)) <= 255)
helper(cur+1);
}
}
}
vector<string> restoreIpAddresses(string s)
{
this->s = s;
this->lens.resize(4);
helper(0);
return res;
}
private:
vector<string> res;
vector<int> lens;
string s;
};
int main(int argc, char const *argv[])
{
Solution sol;
auto fuck = sol.restoreIpAddresses("25525511135");
fuck = sol.restoreIpAddresses("010010");
for(auto e: fuck)
{
cout<<e<<endl;
}
return 0;
}