Skip to content

Commit 10a8e38

Browse files
committed
feat: 添加CodePanel组件用于展示算法代码
1 parent 733d888 commit 10a8e38

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

src/components/CodePanel.tsx

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import React from 'react';
2+
import { AnimationState } from '../state/animationSlice';
3+
4+
interface CodePanelProps {
5+
state: AnimationState;
6+
}
7+
8+
// 为每个算法步骤预定义代码片段
9+
const DP_CODE_STEPS = [
10+
{
11+
title: "问题定义",
12+
code: `/**
13+
* 爬楼梯问题:假设你正在爬楼梯,需要n阶才能到达楼顶。
14+
* 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶?
15+
* @param {number} n - 楼梯的阶数
16+
* @return {number} - 爬到楼顶的方法数
17+
*/`
18+
},
19+
{
20+
title: "动态规划思路",
21+
code: `// 动态规划解法
22+
// 1. 定义状态:dp[i] 表示爬到第i阶的方法数
23+
// 2. 状态转移方程:dp[i] = dp[i-1] + dp[i-2]
24+
// 3. 初始条件:dp[0] = 1, dp[1] = 1
25+
// 4. 计算顺序:从小到大推导`
26+
},
27+
{
28+
title: "基本情况处理",
29+
code: `function climbStairs(n: number): number {
30+
// 处理边界情况
31+
if (n <= 0) return 0;
32+
if (n === 1) return 1;`
33+
},
34+
{
35+
title: "初始化",
36+
code: ` // 初始化DP数组
37+
const dp: number[] = new Array(n + 1).fill(0);
38+
dp[0] = 1; // 不爬楼梯,只有1种方法
39+
dp[1] = 1; // 爬1阶楼梯,只有1种方法(一次爬1阶)`
40+
},
41+
{
42+
title: "状态转移",
43+
code: ` // 状态转移
44+
for (let i = 2; i <= n; i++) {
45+
// 爬到第i阶的方法 = 从第i-1阶爬1阶的方法数 + 从第i-2阶爬2阶的方法数
46+
dp[i] = dp[i-1] + dp[i-2];
47+
}`
48+
},
49+
{
50+
title: "返回结果",
51+
code: ` // 返回爬到第n阶的方法数
52+
return dp[n];
53+
}`
54+
},
55+
{
56+
title: "空间优化",
57+
code: `// 空间优化版本 O(1)空间复杂度
58+
function climbStairsOptimized(n: number): number {
59+
if (n <= 0) return 0;
60+
if (n === 1) return 1;
61+
62+
let p = 1; // dp[0]
63+
let q = 1; // dp[1]
64+
let r = 0; // dp[i]
65+
66+
// 滚动数组计算
67+
for (let i = 2; i <= n; i++) {
68+
r = p + q; // dp[i] = dp[i-2] + dp[i-1]
69+
p = q; // dp[i-2] = dp[i-1]
70+
q = r; // dp[i-1] = dp[i]
71+
}
72+
73+
return r;
74+
}`
75+
}
76+
];
77+
78+
const MATRIX_CODE_STEPS = [
79+
{
80+
title: "矩阵快速幂解法",
81+
code: `/**
82+
* 矩阵快速幂解法爬楼梯问题
83+
* 利用斐波那契数列的性质,用矩阵乘法优化
84+
*/`
85+
},
86+
// ... 可以根据需要添加更多矩阵算法的代码步骤
87+
];
88+
89+
const FORMULA_CODE_STEPS = [
90+
{
91+
title: "数学公式解法",
92+
code: `/**
93+
* 通项公式解法爬楼梯问题
94+
* 利用斐波那契数列的通项公式直接计算结果
95+
*/`
96+
},
97+
// ... 可以根据需要添加更多公式算法的代码步骤
98+
];
99+
100+
const CodePanel: React.FC<CodePanelProps> = ({ state }) => {
101+
// 根据当前算法和步骤选择代码
102+
const getCodeByStep = () => {
103+
const { currentAlgorithm, currentStep } = state;
104+
let codeSteps;
105+
106+
switch (currentAlgorithm) {
107+
case 'dp':
108+
codeSteps = DP_CODE_STEPS;
109+
break;
110+
case 'matrix':
111+
codeSteps = MATRIX_CODE_STEPS;
112+
break;
113+
case 'formula':
114+
codeSteps = FORMULA_CODE_STEPS;
115+
break;
116+
default:
117+
codeSteps = DP_CODE_STEPS;
118+
}
119+
120+
// 计算当前应该显示哪个代码段
121+
let stepIndex = 0;
122+
const totalSteps = state.timeline.length;
123+
124+
if (totalSteps > 0) {
125+
// 将当前步骤映射到代码步骤
126+
const stepRatio = currentStep / totalSteps;
127+
stepIndex = Math.min(Math.floor(stepRatio * codeSteps.length), codeSteps.length - 1);
128+
}
129+
130+
return codeSteps[stepIndex];
131+
};
132+
133+
const codeData = getCodeByStep();
134+
const description = state.timeline[state.currentStep]?.description || "";
135+
136+
// 判断当前描述是否与代码相关
137+
const isCodeRelevant = description.toLowerCase().includes('初始化') ||
138+
description.toLowerCase().includes('计算') ||
139+
description.toLowerCase().includes('状态转移');
140+
141+
return (
142+
<div className="code-panel" style={{
143+
border: '1px solid #ccc',
144+
borderRadius: '4px',
145+
padding: '15px',
146+
backgroundColor: '#f5f5f5',
147+
height: '100%',
148+
overflow: 'auto',
149+
display: 'flex',
150+
flexDirection: 'column'
151+
}}>
152+
<div style={{ marginBottom: '10px', fontWeight: 'bold', borderBottom: '1px solid #ddd', paddingBottom: '5px' }}>
153+
{codeData.title}
154+
</div>
155+
156+
<pre style={{
157+
backgroundColor: '#282c34',
158+
color: '#abb2bf',
159+
padding: '10px',
160+
borderRadius: '4px',
161+
overflow: 'auto',
162+
flex: 1,
163+
fontSize: '14px',
164+
lineHeight: '1.5'
165+
}}>
166+
<code>
167+
{codeData.code}
168+
</code>
169+
</pre>
170+
171+
{isCodeRelevant && (
172+
<div style={{
173+
marginTop: '10px',
174+
padding: '10px',
175+
backgroundColor: '#e3f2fd',
176+
borderRadius: '4px',
177+
borderLeft: '4px solid #2196f3'
178+
}}>
179+
<strong>当前步骤:</strong> {description}
180+
</div>
181+
)}
182+
</div>
183+
);
184+
};
185+
186+
export default CodePanel;

0 commit comments

Comments
 (0)