Add destructors to the C++ codes.

This commit is contained in:
Yudong Jin
2023-01-14 19:52:11 +08:00
parent 87acfc91ab
commit bb657f9517
19 changed files with 121 additions and 24 deletions

View File

@ -56,5 +56,9 @@ int main() {
cout << "方法二 res = ";
PrintUtil::printVector(res);
// 释放内存
delete slt1;
delete slt2;
return 0;
}

View File

@ -18,7 +18,7 @@ void constant(int n) {
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode* node = new ListNode(0);
ListNode node(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++) {
int c = 0;
@ -34,9 +34,9 @@ void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
vector<int> nums(n);
// 长度为 n 的列表占用 O(n) 空间
vector<ListNode*> nodes;
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(new ListNode(i));
nodes.push_back(ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
unordered_map<int, string> map;
@ -98,5 +98,8 @@ int main() {
TreeNode* root = buildTree(n);
PrintUtil::printTree(root);
// 释放内存
freeMemoryTree(root);
return 0;
}