-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetcodeTools.h
executable file
·417 lines (364 loc) · 12 KB
/
LeetcodeTools.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
Leetcode Normal Tools
@Author: loskyer
@Date: 2018/08/22
*/
#ifndef _LEETCODE_H_
#define _LEETCODE_H_
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <iterator>
#include <limits>
#include <climits>
#include <assert.h>
#include <numeric>
#include <bitset>
#include <memory>
#include <cstring>
/*
==> code snippet
#include "LeetcodeTools.h"
using namespace leetcode;
class Solution {
public:
};
int main () {
Solution sol;
return 0;
}
*/
namespace leetcode {
using namespace std;
// ======================================================= For Intervals ====================================================
typedef struct Interval {
int start, end;
Interval(int start, int end) {
this->start = start;
this->end = end;
}
}Interval;
// make Interval can be cout
std::ostream &operator<<(std::ostream &os, const Interval& inv) {
return os << "[" << inv.start << ", " << inv.end << "]";
}
vector<Interval> buildIntervals(vector<int> vec) {
assert(vec.size() % 2 == 0);
vector<Interval> res;
for(int i = 0; i < vec.size() - 1; i += 2) {
res.push_back(Interval(vec[i], vec[i + 1]));
}
return res;
}
// ======================================================= For Trees ========================================================
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
};
// make tree node cout able
std::ostream &operator<<(std::ostream &os, const TreeNode& node) {
string left = node.left ? to_string(node.left->val) : "null";
string right = node.right ? to_string(node.right->val) : "null";
return os << "[" << node.val << "](" << left << ")(" << right << ")";
}
std::ostream &operator<<(std::ostream &os, const TreeNode* nodePtr) {
if(nodePtr == nullptr) {
return os << "null";
}
else {
return os << nodePtr->val;
}
}
/*
Serialize & Deserialize a binary tree, using codec as below:
- Level travsal, '#' for a null
- End with last not null
*/
class TreeCodec {
public:
// Encodes a tree to a single string.
static string serialize(TreeNode* root) {
string res = "[";
queue<TreeNode*> Q; Q.push(root);
int count = (int)(root != nullptr);
while(!Q.empty() && count > 0) {
TreeNode* cur = Q.front(); Q.pop();
if(cur == nullptr) {
res += "#,";
}
else {
count--;
res += to_string(cur->val) + ",";
Q.push(cur->left);
Q.push(cur->right);
count += (int)(cur->left != nullptr) + (int)(cur->right != nullptr);
}
}
if(res.back() == ',') res.pop_back();
res += "]";
return res;
}
// Decodes your encoded data to tree.
static TreeNode* deserialize(string data) {
int l = 1, r = 1;
vector<TreeNode* > prev;
vector<TreeNode* > cur;
TreeNode* root = nullptr;
if(data.size() <= 2) return root;
int k = 0;
while(r < data.size()){
while(r < data.size() && data[r] != ',' && data[r] != ']') r++;
string sval = data.substr(l, r - l);
if(root == nullptr) { // first node, must non-empty
TreeNode* node = new TreeNode( stoi(sval) );
root = node;
cur.push_back(node);
swap(cur, prev);
k = 0;
}
else{
if(k / 2 >= prev.size()) { //上一行的节点用完了
cur.swap(prev);
k = 0;
cur.clear();
}
if(sval != "#") {
TreeNode* node = new TreeNode(stoi(sval));
(k % 2 == 0) ? prev[k / 2]->left = node : prev[k / 2]->right = node;
//cur[k] = node;
cur.push_back(node);
}
k++;
}
l = r + 1; r = l;
}
return root;
}
};
void printTree(TreeNode* p, int indent = 4)
{
if(p != NULL) {
if(p->right) {
printTree(p->right, indent+4);
}
if (indent) {
std::cout << std::setw(indent) << ' ';
}
if (p->right) std::cout<<" /\n" << std::setw(indent) << ' ';
std::cout<< p->val << "\n ";
if(p->left) {
std::cout << std::setw(indent) << ' ' <<" \\\n";
printTree(p->left, indent+4);
}
}
}
// Another way
// Print the arm branches (eg, / \ ) on a line
void printBranches(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque<TreeNode*>& nodesQueue, ostream& out) {
deque<TreeNode*>::const_iterator iter = nodesQueue.begin();
for (int i = 0; i < nodesInThisLevel / 2; i++) {
out << ((i == 0) ? setw(startLen-1) : setw(nodeSpaceLen-2)) << "" << ((*iter++) ? "/" : " ");
out << setw(2*branchLen+2) << "" << ((*iter++) ? "\\" : " ");
}
out << endl;
}
// Print the branches and node (eg, ___10___ )
void printNodes(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque<TreeNode*>& nodesQueue, ostream& out) {
deque<TreeNode*>::const_iterator iter = nodesQueue.begin();
for (int i = 0; i < nodesInThisLevel; i++, iter++) {
out << ((i == 0) ? setw(startLen) : setw(nodeSpaceLen)) << "" << ((*iter && (*iter)->left) ? setfill('_') : setfill(' '));
out << setw(branchLen+2) << ((*iter) ? to_string((*iter)->val) : "");
out << ((*iter && (*iter)->right) ? setfill('_') : setfill(' ')) << setw(branchLen) << "" << setfill(' ');
}
out << endl;
}
// Print the leaves only (just for the bottom row)
void printLeaves(int indentSpace, int level, int nodesInThisLevel, const deque<TreeNode*>& nodesQueue, ostream& out) {
deque<TreeNode*>::const_iterator iter = nodesQueue.begin();
for (int i = 0; i < nodesInThisLevel; i++, iter++) {
out << ((i == 0) ? setw(indentSpace+2) : setw(2*level+2)) << ((*iter) ? to_string((*iter)->val) : "");
}
out << endl;
}
int maxHeight(TreeNode* root) {
if(root == nullptr) return 0;
return max(maxHeight(root->left), maxHeight(root->right)) + 1;
}
// Pretty formatting of a binary tree to the output stream
// @ param
// level Control how wide you want the tree to sparse (eg, level 1 has the minimum space between nodes, while level 2 has a larger space between nodes)
// indentSpace Change this to add some indent space to the left (eg, indentSpace of 0 means the lowest level of the left node will stick to the left margin)
void printPretty(TreeNode *root, int level = 1, int indentSpace = 0, ostream& out = std::cout) {
int h = maxHeight(root);
int nodesInThisLevel = 1;
int branchLen = 2*((int)pow(2.0,h)-1) - (3-level)*(int)pow(2.0,h-1); // eq of the length of branch for each node of each level
int nodeSpaceLen = 2 + (level+1)*(int)pow(2.0,h); // distance between left neighbor node's right arm and right neighbor node's left arm
int startLen = branchLen + (3-level) + indentSpace; // starting space to the first node to print of each level (for the left most node of each level only)
deque<TreeNode*> nodesQueue;
nodesQueue.push_back(root);
for (int r = 1; r < h; r++) {
printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out);
branchLen = branchLen/2 - 1;
nodeSpaceLen = nodeSpaceLen/2 + 1;
startLen = branchLen + (3-level) + indentSpace;
printNodes(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out);
for (int i = 0; i < nodesInThisLevel; i++) {
TreeNode *currNode = nodesQueue.front();
nodesQueue.pop_front();
if (currNode) {
nodesQueue.push_back(currNode->left);
nodesQueue.push_back(currNode->right);
} else {
nodesQueue.push_back(NULL);
nodesQueue.push_back(NULL);
}
}
nodesInThisLevel *= 2;
}
printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out);
printLeaves(indentSpace, level, nodesInThisLevel, nodesQueue, out);
}
// ======================================================= For ListNode ======================================================
struct ListNode {
int val;
ListNode* next;
ListNode(int v) : val(v), next(nullptr){}
};
ListNode* buildList(vector<int> dat) {
ListNode dummy(-1);
ListNode* pLast = &dummy;
for(int i = 0; i < dat.size(); i++) {
pLast->next = new ListNode(dat[i]);
pLast = pLast->next;
}
return dummy.next;
}
void printList(ListNode* cur){
while(cur != nullptr) {
cout << cur->val << " -> ";
cur = cur->next;
}
cout << "null" << endl;
}
std::ostream &operator<<(std::ostream &os, const ListNode* nodePtr) {
if(nodePtr == nullptr) {
return os << "null";
}
else {
return os << nodePtr->val;
}
}
// =========================================================== Utility print =======================================
template<typename T>
void printVector(vector<T> vec, string split = " ") {
std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(std::cout, split.c_str()));
cout << endl;
}
template<typename T>
void printVector2D(vector<vector<T>> vec, string split = " ") {
for(int i = 0; i < vec.size(); i++) {
printVector(vec[i], split);
}
}
// C++ template to print vector container elements
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "[";
for (int i = 0; i < v.size(); ++i) {
os << v[i];
if (i != v.size() - 1)
os << ", ";
}
os << "]\n";
return os;
}
// C++ template to print list container elements
template <typename T>
ostream& operator<<(ostream& os, const list<T>& ls)
{
for(auto& x : ls){
cout << x << " -> ";
}
os << "NULL\n";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
os << "[";
for (auto it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "]\n";
return os;
}
// C++ template to print map container elements
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
for (auto it : v)
os << it.first << " : "
<< it.second << "\n";
return os;
}
// C++ template to print pair<>
// class by using template
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
os << "(";
os << v.first << ", "
<< v.second << ")";
return os;
}
// Notice: Pass value here!!
template<typename T>
void printStack(stack<T> stk, string split = " ") {
cout << "[ ";
while(!stk.empty()) {
cout << stk.top() << split;
stk.pop();
}
cout << ">";
cout << endl;
}
// Notice: Pass value here!!
template<typename T>
void printQueue(queue<T> que, string split = " ") {
cout << "< ";
while(!que.empty()) {
cout << que.front() << split;
que.pop();
}
cout << "]";
cout << endl;
}
// ============================================= For Point ==============================================================
struct Point {
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
};
}
#endif