-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.h
185 lines (149 loc) · 4.06 KB
/
Helper.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) luckystone60 Technologies Co., Ltd. 2012-2018. All right reserved.
* Description: Helper.h
* Author:luckystone60
* Create:2020-03-01
*/
#ifndef LEETCODE_H
#define LEETCODE_H
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <regex>
#include <sstream>
namespace LEETCODE
{
namespace HELPER
{
/*******************************
0. data structure
*******************************/
// list Node
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// tree Node
const int32_t TREE_NODE_BOUNDNARY = INT_MIN + 1; // replace null
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x = 0) : val(x), left(NULL), right(NULL) {}
};
/*******************************
1. create structure
*******************************/
template<typename T>
std::vector<T> CreateVector(const std::string inputStr)
{
std::vector<T> ret;
const std::string pattern = R"(([+|-]?\w*\.?\w+),?)";
std::smatch match;
std::string log = inputStr;
while(regex_search(log, match, (std::regex)pattern)) {
// std::cout << match.str(1) << '\n';
std::stringstream ss;
std::string treeNull = match.str(1) == "null" ? std::to_string(TREE_NODE_BOUNDNARY) : match.str(1);
ss << treeNull;
T temp;
ss >> temp;
ret.push_back(temp);
log = match.suffix();
}
if (ret.empty()) {
std::cout << "CreateVector failed." << std::endl;
}
return ret;
}
template<typename T>
std::vector<std::vector<T>> CreateMatrix(const std::string inputStr)
{
std::vector<std::vector<T>> ret;
const std::string pattern = R"((\[[^\[\]]*\]))";
std::smatch match;
std::string log = inputStr;
while(regex_search(log, match, (std::regex)pattern)) {
std::string vec = match.str(1);
ret.push_back(CreateVector<T>(vec));
log = match.suffix();
}
if (ret.empty()) {
std::cout << "CreateVector failed." << std::endl;
}
return ret;
}
ListNode *CreateListImpl(const std::vector<int> &vec);
ListNode *CreateList(const std::string &inputStr);
void DestroyList(ListNode *head);
TreeNode *CreateTreeImpl(const std::vector<int> &vec);
TreeNode *CreateTree(const std::string &input);
void DestroyTree(TreeNode *root);
/*******************************
2. print funcitons
*******************************/
template<typename T>
void PrintValue(const T &value, const std::string message = "PrintValue")
{
std::cout << message << " " << value << std::endl;
}
template<typename T>
void PrintVector(const std::vector<T> &vec, const std::string message = "PrintVector", bool showMessage = true, bool endWithEnter = true)
{
if (showMessage) {
std::cout << message << ": " << std::endl;
}
std::cout << "[";
int32_t count = 0;
for (auto &&i : vec) {
if (count == 0) {
std::cout << i;
} else {
std::cout << "," << i;
}
count++;
}
std::cout << "]";
if (endWithEnter) {
std::cout << std::endl;
}
return;
}
template<typename T>
void PrintMatrix(const std::vector<std::vector<T>> &matrix, const std::string message = "PrintMatrix")
{
std::cout << message << ": " << std::endl;
std::cout << "[";
int32_t count = 0;
for (auto &&i : matrix) {
if (count == 0) {
PrintVector(i, "", false, false);
} else {
std::cout << ",";
PrintVector(i, "", false, false);
}
count++;
}
std::cout << "]" << std::endl;
}
std::vector<int> TransListToVector(ListNode *head);
void PrintList(ListNode *head);
void PrintTreeImpl(TreeNode *n, bool left, std::string const &indent);
void PrintTree(TreeNode *root);
/*******************************
3. others
*******************************/
TreeNode *GetSpecifiedValueNode(TreeNode *root, int32_t value);
} // namespace HELPER
} // namespace LEETCODE
#endif //LEETCODE_H