-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.hpp
441 lines (422 loc) · 11.2 KB
/
leetcode.hpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <istream>
#include <ostream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <vector>
//structures
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Node { //class only for auto complete
public:
int val;
std::vector<Node*> children;
Node* left;
Node* right;
Node* next;
Node(int _val, std::vector<Node*> _children) {
val = _val;
children = _children;
}
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
class NodeChildren { //Called Node in leetcode but there are multiple vairant
public:
int val;
std::vector<NodeChildren*> children;
NodeChildren() {}
NodeChildren(int _val) {
val = _val;
}
NodeChildren(int _val, std::vector<NodeChildren*> _children) {
val = _val;
children = _children;
}
};
class NodeLeftRightNext {//Called Node in leetcode but there are multiple vairant
public:
int val;
NodeLeftRightNext* left;
NodeLeftRightNext* right;
NodeLeftRightNext* next;
NodeLeftRightNext() : val(0), left(NULL), right(NULL), next(NULL) {}
NodeLeftRightNext(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
NodeLeftRightNext(int _val, NodeLeftRightNext* _left, NodeLeftRightNext* _right, NodeLeftRightNext* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
//vector output
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if(v.size()==0){ //catch size equal 0
out <<"[]";
return out;
}
out << '[';
bool ischar = std::is_same<char,T>::value;//check if it is a vector<char>
for(auto x:v){
out << (ischar?"\"":"") << x << (ischar?"\"":"") << ",";//ouput 'char'/int
}
out << "\b]"; // use two ANSI backspace characters '\b' to overwrite final ", "
return out;
}
//vector input
//for vector<char>
std::istream& operator>> (std::istream& in, std::vector<char>& v) {
v.clear();
char c = in.peek();
while(c=='\n'||c==' '){
in.get();
c = in.peek();
}
std::string str;
std::getline(in,str,']');
v.reserve(str.size()/4);
for(std::string::iterator it=str.begin();it<str.end();++it){
if(*it == '\"' || *it == ','|| *it == '['||*it=='\'')
continue;
v.push_back(*it);
}
return in;
}
//for vector<int>
std::istream& operator>> (std::istream& in, std::vector<int>& v) {
v.clear();
char c = in.peek();
while(c=='\n'||c==' '){
in.get();
c = in.peek();
}
std::string str;
std::getline(in,str,']');
std::stringstream os;
for(std::string::iterator it=str.begin();it<str.end();++it){
if(*it == '[' )
continue;
if(*it == ','||it==str.end()-1){
int temp;
if(it==str.end()-1)// append the last char for this case
os << *it;
os >> temp;
v.push_back(temp);
os.clear();
continue;
}
if(*it>'9'||*it<'0')
continue;
os << *it;
}
return in;
}
//for vector<bool>
std::istream& operator>> (std::istream& in, std::vector<bool>& v) {
v.clear();
char c = in.peek();
while(c=='\n'||c==' '){
in.get();
c = in.peek();
}
std::string str;
std::getline(in,str,']');
for(char x:str){
if(x=='f')
v.push_back(false);
else if(x=='t')
v.push_back(true);
}
return in;
}
template <class T>
std::istream& operator>> (std::istream& in, std::vector<T>& v) {
v.clear();
char c = in.peek();
while(c=='\n'||c==' '){
in.get();
c = in.peek();
}
std::string str;
int count =0;
//take input
do{
char x = in.peek();
if(x=='[')
count++;
if(x==']')
count--;
if(count>=0)
str += in.get();
}while(count >0);
count =0;
for(char x:str)
if(x == '[') count++;
else break;
count--;
std::string sample(count,']');
size_t lst = 1;
size_t pos = str.find(sample);
while(pos!=std::string::npos&&pos-lst>=count){
T temp;
std::string tmp = std::string(str,lst,pos+count-1);
std::stringstream os(tmp);
os >> temp;
v.push_back(temp);
lst = pos+count+1;
pos = str.find(sample,lst);
}
return in;
}
//output TreeNode
//cannot use TreeNode* as std lib will simpily bypass the custom function and output the address
std::ostream& operator<< (std::ostream& out, const TreeNode & root) {
out << "[";
std::queue<TreeNode*> q;
TreeNode* r = new TreeNode(root.val,root.left,root.right);
if(r)
q.push(r);
int lstNull=0;
while(!q.empty()){
int i=0;
std::queue<TreeNode*> q2 = q;
while(!q2.empty()){//check if this height of the tree only contains null
if(q2.front()!=nullptr)
break;
q2.pop();
i++;
}
if(i==q.size())
break;
for(int size=q.size();size>0;size--){
TreeNode* node = q.front();
if(node==nullptr){
q.pop();
out << "null,";
lstNull++;
continue;
}
q.pop();
out << node->val << ",";
lstNull=0;
q.push(node->left);
q.push(node->right);
}
}
out << std::string(5*lstNull,'\b')+"\b]";//n * 5 * \b delete the last n null and the last comma
return out;
}
//input TreeNode
std::istream& operator>> (std::istream& in, TreeNode* & root) {
char c = in.peek();
while(c=='\n'||c==' '){
in.get();
c = in.peek();
}
std::string str;
std::getline(in,str,']');
if(str.size()==1){
root = nullptr;
return in;
}
size_t pos = str.find(',');
if(pos==std::string::npos)
pos = str.size();
size_t lst = 1;
std::queue<TreeNode*> q;
std::queue<std::pair<TreeNode*,bool>> parent;//parent, and left or right
root = new TreeNode();
q.push(root);
parent.push({nullptr,true});
while(lst<=pos){
std::string s(str,lst,pos-lst);
if(s=="null"){
delete q.front();
if(parent.front().second){
parent.front().first->left = nullptr;
}else{
parent.front().first->right = nullptr;
}
}else {
int x = stoi(s);
q.front()->val = x;
q.front()->left = new TreeNode();
q.front()->right= new TreeNode();
q.push(q.front()->left);q.push(q.front()->right);
parent.push({q.front(),true}),parent.push({q.front(),false});
}
parent.pop();
q.pop();
lst = pos+1;
pos = str.find(',',pos+1);
if(pos==std::string::npos)
pos = str.size();
}
while(!q.empty()){
delete q.front();
if(parent.front().second){
parent.front().first->left = nullptr;
}else{
parent.front().first->right = nullptr;
}
parent.pop();
q.pop();
TreeNode node = *root;
}
return in;
}
//output ListNode
//cannot use ListNode* as std lib will simpily bypass the custom function and output the address
std::ostream& operator<< (std::ostream& out, const ListNode list) {
out << "[";
ListNode* r = new ListNode(list.val,list.next);
while(r){
out << r->val << ",";
r = r->next;
}
out <<"\b]";
return out;
}
// input ListNode
std::istream& operator>> (std::istream& in, ListNode*& list) {
char c = in.peek();
while(c=='\n'||c==' ')
c = in.get();
std::string str;
std::getline(in,str,']');
if(str.size()==1){
list = nullptr;
return in;
}
std::stringstream os;
ListNode* node = list;
bool first =true;
for(std::string::iterator it=str.begin();it<str.end();++it){
if(*it == '[' )
continue;
if(*it == ','||it==str.end()-1){
int temp;
if(it==str.end()-1)// append the last char for this case
os << *it;
os >> temp;
if(first){
node->val = temp;
first = false;
}else{
node->next = new ListNode(temp);
node = node->next;
}
os.clear();
continue;
}
if(*it>'9'||*it<'0')
continue;
os << *it;
}
return in;
}
//output NodeChildren variant of node (called node in leetcode)
// beware that a char before the output will be deleted
//cannot use NodeChildren * as std lib will simpily bypass the custom function and output the address
std::ostream& operator<< (std::ostream& out, const NodeChildren node) {
int lstNull = 0;
out << "[";
out << node.val;
out << ",null,";
lstNull++;
std::vector<NodeChildren*> nodes = node.children;
std::queue<std::vector<NodeChildren*>> q;
std::queue<int> num;
q.push(node.children);
num.push(1);
num.push(node.children.size());
while(!q.empty()){
std::queue<std::vector<NodeChildren*>> tempq = q;
while(!tempq.empty()&&tempq.front().size()==0){
tempq.pop();
}
if(tempq.size()==0)
break;
int temp = num.front();
num.pop();
for(int i = 0;i<temp;i++){
std::vector<NodeChildren*> tempNode = q.front();
q.pop();
if(tempNode.size()==0){
out << "null,";
lstNull++;
}
for(auto x:tempNode){
out << x->val << ",";
lstNull=0;
q.push(x->children);
num.push(x->children.size());
}
if(tempNode.size()!=0){
out << "null,";
lstNull++;
}
}
}
out << std::string(5*lstNull,'\b')+"\b]";//n * 5 * \b delete the last n null and the last comma
return out;
}
//input NodeChildren
std::istream& operator>> (std::istream& in, NodeChildren*& node) {
char c = in.peek();
while(c=='\n'||c==' '){
in.get();
c = in.peek();
}
std::string str;
std::getline(in,str,']');
if(str.size()==1){
node = nullptr;
return in;
}
node = new NodeChildren();
size_t pos = str.find(',');
if(pos==std::string::npos)
pos = str.size();
size_t lst = 1;
bool start = true;
std::queue<NodeChildren*> parent;
while(lst<=pos){
std::string s(str,lst,pos-lst);
if(parent.empty()){
int x = stoi(s);
node->val = x;
parent.push(node);
}else if(s=="null"){
if(!start) parent.pop();
else start = false;
}else {
int x = stoi(s);
NodeChildren * newnode = new NodeChildren();
parent.front()->children.push_back(newnode);
newnode->val = x;
parent.push(newnode);
}
lst = pos+1;
pos = str.find(',',pos+1);
if(pos==std::string::npos)
pos = str.size();
}
return in;
}