vault backup: 2026-06-10 11:19:43
This commit is contained in:
+26
-19
@@ -48,14 +48,20 @@ create time: 2026-06-10 11:11
|
||||
2. **流程图**:图形化表示,直观清晰。注意:**菱形表示判断/选择**
|
||||
3. **伪代码**:介于自然语言和编程语言之间,兼顾可读性和精确性
|
||||
|
||||
```plaintext
|
||||
// 伪代码示例:查找数组中的最小值
|
||||
function findMin(A, n):
|
||||
minVal = A[1]
|
||||
for i = 2 to n:
|
||||
if A[i] < minVal:
|
||||
minVal = A[i]
|
||||
return minVal
|
||||
```cpp
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
// 查找数组中的最小值
|
||||
int findMin(vector<int>& A, int n) {
|
||||
int minVal = A[0];
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (A[i] < minVal) {
|
||||
minVal = A[i];
|
||||
}
|
||||
}
|
||||
return minVal;
|
||||
}
|
||||
```
|
||||
|
||||
> [!tip] 流程图关键符号
|
||||
@@ -161,17 +167,18 @@ T(0) = 0, T(1) = 0
|
||||
- 每次返回,从栈顶弹出恢复现场
|
||||
- **DFS(深度优先搜索)** 也使用栈实现
|
||||
|
||||
```plaintext
|
||||
// Fibonacci的迭代版本(使用栈模拟的思想,但实际是循环)
|
||||
function fibIterative(n):
|
||||
if n <= 1: return n
|
||||
prev = 0
|
||||
curr = 1
|
||||
for i = 2 to n:
|
||||
next = prev + curr
|
||||
prev = curr
|
||||
curr = next
|
||||
return curr
|
||||
```cpp
|
||||
// Fibonacci的迭代版本(时间O(n),空间O(1))
|
||||
int fibIterative(int n) {
|
||||
if (n <= 1) return n;
|
||||
int prev = 0, curr = 1;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
int next = prev + curr;
|
||||
prev = curr;
|
||||
curr = next;
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
```
|
||||
|
||||
迭代版本将时间复杂度从 O(2ⁿ) 降至 O(n),空间复杂度从 O(n)(递归栈)降至 O(1)。
|
||||
|
||||
Reference in New Issue
Block a user