-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.h
113 lines (97 loc) · 2.43 KB
/
leetcode.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
#pragma once
#include <bits/stdc++.h>
struct ListNode {
int value = 0;
ListNode *next = nullptr;
explicit ListNode(int val) : value(val) {}
};
struct TreeNode {
int value = 0;
TreeNode *left = nullptr;
TreeNode *right = nullptr;
};
namespace utils {
template <typename Fn>
void foreach (ListNode *n, Fn && fn) {
while (n != nullptr) {
fn(n->value);
n = n->next;
}
}
template <typename T = int, typename TIterable = std::initializer_list<T>>
ListNode *make_nodes(TIterable &&iterable) {
auto end = std::end(iterable);
auto iter = std::begin(iterable);
if (iter == end) {
return nullptr;
}
auto *head = new ListNode(*iter++);
for (ListNode *p = head; iter != end; ++iter, p = p->next) {
p->next = new ListNode(static_cast<int>(*iter));
}
return head;
}
inline void del_nodes(ListNode *n) {
for (ListNode *next; n != nullptr; n = next) {
next = n->next;
delete n;
}
}
inline void print_nodes(ListNode *n) {
if (n == nullptr) {
std::cout << "[]" << std::endl;
return;
}
while (n->next != nullptr) {
std::cout << "[" << n->value << "] -> ";
n = n->next;
}
std::cout << "[" << n->value << "]" << std::endl;
}
inline TreeNode *make_tree(const int v, TreeNode *ln = nullptr, TreeNode *rn = nullptr) {
return new TreeNode{v, ln, rn};
}
inline void del_tree(TreeNode *n) {
if (n == nullptr) {
return;
}
del_tree(n->left);
del_tree(n->right);
delete n;
}
template <typename TIterable>
void print(TIterable &&iterable) {
auto end = std::end(iterable);
auto iter = std::begin(iterable);
std::cout << "[";
if (iter != end) {
std::cout << *iter;
++iter;
}
while (iter != end) {
std::cout << ", " << *iter;
++iter;
}
std::cout << "]";
}
inline void print(std::ostream &out, const int it) {
out << std::setw(4) << it;
}
inline void print(std::ostream &out, const bool it) {
out << std::setw(4) << (it ? "O" : "X");
}
template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &vec) {
print(vec);
return out;
}
template <typename T>
void operator<<(std::ostream &out, const std::vector<std::vector<T>> &mat) {
for (auto &&vec : mat) {
for (auto &&v : vec) {
print(out, v);
}
out << std::endl;
}
}
} // namespace utils