108 lines
2.1 KiB
C++
108 lines
2.1 KiB
C++
|
/**
|
|||
|
* File: space_complexity.cpp
|
|||
|
* Created Time: 2022-11-25
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD> */
|
|||
|
int func() {
|
|||
|
// ִ<><D6B4>ijЩ<C4B3><D0A9><EFBFBD><EFBFBD>
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
void constant(int n) {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC> O(1) <20>ռ<EFBFBD>
|
|||
|
const int a = 0;
|
|||
|
int b = 0;
|
|||
|
vector<int> nums(10000);
|
|||
|
ListNode node(0);
|
|||
|
// ѭ<><D1AD><EFBFBD>еı<D0B5><C4B1><EFBFBD>ռ<EFBFBD><D5BC> O(1) <20>ռ<EFBFBD>
|
|||
|
for (int i = 0; i < n; i++) {
|
|||
|
int c = 0;
|
|||
|
}
|
|||
|
// ѭ<><D1AD><EFBFBD>еĺ<D0B5><C4BA><EFBFBD>ռ<EFBFBD><D5BC> O(1) <20>ռ<EFBFBD>
|
|||
|
for (int i = 0; i < n; i++) {
|
|||
|
func();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>Խ<EFBFBD> */
|
|||
|
void linear(int n) {
|
|||
|
// <20><><EFBFBD><EFBFBD>Ϊ n <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC> O(n) <20>ռ<EFBFBD>
|
|||
|
vector<int> nums(n);
|
|||
|
// <20><><EFBFBD><EFBFBD>Ϊ n <20><><EFBFBD>б<EFBFBD>ռ<EFBFBD><D5BC> O(n) <20>ռ<EFBFBD>
|
|||
|
vector<ListNode> nodes;
|
|||
|
for (int i = 0; i < n; i++) {
|
|||
|
nodes.push_back(ListNode(i));
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD>Ϊ n <20>Ĺ<EFBFBD>ϣ<EFBFBD><CFA3>ռ<EFBFBD><D5BC> O(n) <20>ռ<EFBFBD>
|
|||
|
unordered_map<int, string> map;
|
|||
|
for (int i = 0; i < n; i++) {
|
|||
|
map[i] = to_string(i);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>Խף<D4BD><D7A3>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD> */
|
|||
|
void linearRecur(int n) {
|
|||
|
cout << "<EFBFBD>ݹ<EFBFBD> n = " << n << endl;
|
|||
|
if (n == 1)
|
|||
|
return;
|
|||
|
linearRecur(n - 1);
|
|||
|
}
|
|||
|
|
|||
|
/* ƽ<><C6BD><EFBFBD><EFBFBD> */
|
|||
|
void quadratic(int n) {
|
|||
|
// <20><>ά<EFBFBD>б<EFBFBD>ռ<EFBFBD><D5BC> O(n^2) <20>ռ<EFBFBD>
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* ƽ<><C6BD><EFBFBD>ף<EFBFBD><D7A3>ݹ<EFBFBD>ʵ<EFBFBD>֣<EFBFBD> */
|
|||
|
int quadraticRecur(int n) {
|
|||
|
if (n <= 0)
|
|||
|
return 0;
|
|||
|
vector<int> nums(n);
|
|||
|
cout << "<EFBFBD>ݹ<EFBFBD> n = " << n << " <20>е<EFBFBD> nums <20><><EFBFBD><EFBFBD> = " << nums.size() << endl;
|
|||
|
return quadraticRecur(n - 1);
|
|||
|
}
|
|||
|
|
|||
|
/* ָ<><D6B8><EFBFBD>ף<EFBFBD><D7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
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;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
constant(n);
|
|||
|
// <20><><EFBFBD>Խ<EFBFBD>
|
|||
|
linear(n);
|
|||
|
linearRecur(n);
|
|||
|
// ƽ<><C6BD><EFBFBD><EFBFBD>
|
|||
|
quadratic(n);
|
|||
|
quadraticRecur(n);
|
|||
|
// ָ<><D6B8><EFBFBD><EFBFBD>
|
|||
|
TreeNode *root = buildTree(n);
|
|||
|
printTree(root);
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
freeMemoryTree(root);
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|