mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 15:01:58 +08:00
Update time complexity and space complexity.
This commit is contained in:
102
codes/cpp/chapter_computational_complexity/space_complexity.cpp
Normal file
102
codes/cpp/chapter_computational_complexity/space_complexity.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* File: space_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 函数 */
|
||||
int func() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
void constant(int n) {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode* node = new ListNode(0);
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
void linear(int n) {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
vector<int> nums(n);
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
vector<ListNode*> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(new ListNode(i));
|
||||
}
|
||||
// 长度为 n 的哈希表占用 O(n) 空间
|
||||
unordered_map<int, string> map;
|
||||
for (int i = 0; i < n; i++) {
|
||||
map[i] = to_string(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.push_back(0);
|
||||
}
|
||||
numMatrix.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode* buildTree(int n) {
|
||||
if (n == 0) return nullptr;
|
||||
TreeNode* root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
// 线性阶
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 平方阶
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数阶
|
||||
TreeNode* root = buildTree(n);
|
||||
PrintUtil::printTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user