mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Add cpp codes for the chapter
computational complexity, sorting, searching.
This commit is contained in:
@ -56,7 +56,26 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
/* 结构体 */
|
||||
struct Node {
|
||||
int val;
|
||||
Node *next;
|
||||
Node(int x) : val(x), next(nullptr) {}
|
||||
};
|
||||
|
||||
/* 函数(或称方法) */
|
||||
int func() {
|
||||
// do something...
|
||||
return 0;
|
||||
}
|
||||
|
||||
int algorithm(int n) { // 输入数据
|
||||
const int a = 0; // 暂存数据(常量)
|
||||
int b = 0; // 暂存数据(变量)
|
||||
Node* node = new Node(0); // 暂存数据(对象)
|
||||
int c = func(); // 栈帧空间(调用函数)
|
||||
return a + b + c; // 输出数据
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -74,7 +93,6 @@ comments: true
|
||||
return 0
|
||||
|
||||
def algorithm(n): # 输入数据
|
||||
a = 0 # 暂存数据(常量)
|
||||
b = 0 # 暂存数据(变量)
|
||||
node = Node(0) # 暂存数据(对象)
|
||||
c = function() # 栈帧空间(调用函数)
|
||||
@ -104,7 +122,12 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
vector<int> b(10000); // O(1)
|
||||
if (n > 10)
|
||||
vector<int> nums(n); // O(n)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -142,7 +165,21 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
|
||||
int func() {
|
||||
// do something
|
||||
return 0;
|
||||
}
|
||||
/* 循环 O(1) */
|
||||
void loop(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
/* 递归 O(n) */
|
||||
void recur(int n) {
|
||||
if (n == 1) return;
|
||||
return recur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -212,7 +249,22 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 常数阶 */
|
||||
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();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -259,7 +311,21 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 线性阶 */
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -291,7 +357,12 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -318,7 +389,7 @@ $$
|
||||
/* 平方阶 */
|
||||
void quadratic(int n) {
|
||||
// 矩阵占用 O(n^2) 空间
|
||||
int numMatrix[][] = new int[n][n];
|
||||
int [][]numMatrix = new int[n][n];
|
||||
// 二维列表占用 O(n^2) 空间
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -334,7 +405,18 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 平方阶 */
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -363,7 +445,13 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -401,7 +489,14 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="space_complexity_types.cpp"
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
Reference in New Issue
Block a user