mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-06 22:34:18 +08:00
Add destructors to the C++ codes.
This commit is contained in:
@ -56,5 +56,9 @@ int main() {
|
||||
cout << "方法二 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
// 释放内存
|
||||
delete slt1;
|
||||
delete slt2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user