-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path93.restore-ip-addresses.cpp
89 lines (73 loc) · 2.22 KB
/
93.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
77
78
79
80
81
82
83
84
85
86
87
88
/*
* @lc app=leetcode id=93 lang=cpp
*
* [93] Restore IP Addresses
*/
// @lc code=start
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
bool isValidIp (string str) {
if (str.size() < 7) return false;
string nums[4] = {"", "", "", ""};
char spliter = '.';
int numsIdx = 0;
string buf = "";
for (int i = 0; i < str.size(); i++) {
if (i == str.size() - 1) {
if (buf.size() > 4) return false;
nums[3] = buf + str.at(i);
break;
}
if (str.at(i) == spliter) {
if (buf.size() > 4 || stoi(buf) > 255) return false;
nums[numsIdx++] = buf;
buf = "";
}
else {
buf += str.at(i);
}
}
for (int i = 0; i < 4; i++) {
if (stoi(nums[i]) > 255 || nums[i] == "" || (nums[i].at(0) == '0' && nums[i].size() != 1) ) return false;
}
return true;
}
vector<string> restoreIpAddresses(string s) {
if (s.size() > 12) {
return vector<string> {};
}
vector<string> outputs;
stack<pair<string, int>> stk;
int strIdx = 0;
string str(string(1, s[strIdx]));
stk.push({ str, 0 });
while (!stk.empty()) {
pair<string, int> top = stk.top();
stk.pop();
if (top.second >= 3) {
string remainingStr = s.substr(top.first.size() - 3, -1);
if (isValidIp(top.first + remainingStr))
{
outputs.push_back(top.first + remainingStr);
}
} else {
if (top.first.size() - top.second < s.size()) {
if (top.first.at(top.first.size() - 1) != '.') {
stk.push({ top.first + "." , top.second + 1 });
}
stk.push({ top.first + string(1, s[top.first.size() - top.second]) , top.second });
}
}
}
return outputs;
}
};
// @lc code=end