Files
illustrated-algorithm/climbing-stairs/index.html
T
2026-08-24 04:35:13 +00:00

177 lines
7.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>081. 爬楼梯 – 图解</title>
<link rel="stylesheet" href="../shared/style.css">
<style>
/* page-specific overrides */
.vis-area { min-height: 120px; padding: 16px 0; }
.code-section { margin-top: 16px; }
</style>
</head>
<body>
<div class="container">
<h1>🟢 081. 爬楼梯 <span class="badge easy">简单</span></h1>
<p class="subtitle">分类:动态规划 | LeetCode Hot 100</p>
<!-- 控制面板 -->
<div class="controls" id="controls">
<label for="inputArea">输入:</label>
<input type="text" id="inputArea" placeholder="默认示例,可自定义">
<button id="applyBtn" class="primary">生成图解</button>
<select id="exampleSelect"></select>
<span style="flex:1"></span>
<button id="prevBtn">◀ 上一步</button>
<button id="nextBtn">下一步 ▶</button>
<button id="jumpBtn">⏭ 跳到结果</button>
<button id="autoBtn">自动播放</button>
<button id="resetBtn">重置</button>
</div>
<!-- 步骤流水线 -->
<div class="pipeline" id="pipeline"></div>
<!-- 提示条 -->
<div class="hint info" id="hintBox">
<span id="stepInfo"></span><br>
<span id="hintText"></span>
</div>
<!-- 可视化面板 -->
<div class="panels">
<div class="panel" id="mainPanel">
<h3>📊 可视化</h3>
<div class="vis-area" id="vizArea">点击「生成图解」开始</div>
</div>
<div class="panel-grid">
<div class="panel" id="detailPanel">
<h3>📝 当前步骤详情</h3>
<div id="detailContent">等待开始...</div>
</div>
<div class="panel" id="resultPanel">
<h3>✅ 结果</h3>
<div id="resultContent">等待完成...</div>
</div>
</div>
</div>
<!-- 代码 -->
<div class="panel code-section">
<h3>💻 参考代码(Python)</h3>
<div id="codeArea"></div>
</div>
<footer>Powered by QwenPaw · 图解算法 · LeetCode Hot 100</footer>
</div>
<script src="../shared/algo-viz.js"></script>
<script>
"use strict";
(function() {
// ========== Algorithm Logic ==========
const examples = [
{input: 5, label: '示例1: n=5'},
{input: 3, label: '示例2: n=3'},
{input: 10, label: '示例3: n=10'},
];
let steps, stepCtrl, n;
function buildSteps(total) {
n = total; steps = [];
const dp = new Array(n + 1).fill(0);
dp[0] = 0; dp[1] = 1; dp[2] = 2;
steps.push({stage:'init', msg:'初始化:dp[1]=1(1种),dp[2]=2(2种)', dp:[...dp], current:-1, from1:-1, from2:-1});
for (let i = 3; i <= n; i++) {
steps.push({stage:'calc', msg:`计算 dp[${i}] = dp[${i-1}] + dp[${i-2}] = ${dp[i-1]} + ${dp[i-2]} = ${dp[i-1]+dp[i-2]}`, dp:[...dp], current:i, from1:i-1, from2:i-2});
dp[i] = dp[i-1] + dp[i-2];
steps.push({stage:'fill', msg:`dp[${i}] = ${dp[i]},已填充`, dp:[...dp], current:i, from1:i-1, from2:i-2});
}
steps.push({stage:'done', msg:`结果:爬到第 ${n} 阶有 ${dp[n]} 种方法`, dp:[...dp], current:n, from1:-1, from2:-1});
}
function render(step) {
const s = steps[step];
let viz = '<div style="display:flex;align-items:flex-end;gap:3px;margin:12px 0 8px;">';
for (let i = 1; i <= n; i++) {
const filled = s.dp[i] > 0;
const isCurrent = i === s.current;
const isFrom1 = i === s.from1;
const isFrom2 = i === s.from2;
let bg = '#e2e8f0', color = '#64748b';
if (isCurrent && (s.stage==='fill'||s.stage==='done')) { bg = 'var(--green)'; color = 'white'; }
else if (isCurrent) { bg = 'var(--orange)'; color = 'white'; }
else if (isFrom1) { bg = '#dbeafe'; color = '#1e40af'; }
else if (isFrom2) { bg = '#ede9fe'; color = '#5b21b6'; }
else if (filled) { bg = '#f0fdf4'; color = '#166534'; }
viz += `<div style="width:42px;height:${36+i*6}px;background:${bg};border-radius:6px 6px 0 0;display:flex;flex-direction:column;align-items:center;justify-content:flex-end;padding:4px 0;font-size:11px;font-weight:600;color:${color};transition:all 0.3s;">
<span style="font-size:13px;">${s.dp[i]||''}</span><span style="font-size:9px;opacity:0.7;">${i}</span></div>`;
}
viz += '</div>';
viz += '<div style="margin-top:12px;"><b>dp 表:</b></div>';
const dpHl = {};
if (s.current > 0) dpHl[s.current-1] = s.stage==='fill'?'green':'orange';
if (s.from1 > 0) dpHl[s.from1-1] = 'blue';
if (s.from2 > 0) dpHl[s.from2-1] = 'purple';
viz += renderArray(s.dp.slice(1), {highlights: dpHl});
if (s.stage === 'calc') {
viz += `<div class="formula-box" style="margin-top:8px;">dp[${s.current}] = dp[${s.from1}] + dp[${s.from2}] = ${s.dp[s.from1]} + ${s.dp[s.from2]}</div>`;
}
$('vizArea').innerHTML = viz;
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
if (s.stage !== 'done' && s.current > 0) {
$('detailContent').innerHTML += `<div class="current-answer">dp[${s.current}] = <b>${s.dp[s.current]}</b></div>`;
}
if (s.stage === 'done') {
$('resultContent').innerHTML = `<div class="final-answer">爬到第 ${n} 阶有 <b>${s.dp[n]}</b> 种方法</div>`;
}
$('hintText').textContent = s.msg;
const stages = ['init→初始化','calc→递推计算','fill→填充','done→完成'];
$('pipeline').innerHTML = stages.map(st => {
const [k,l] = st.split('→');
return `<span class="pipe-step ${s.stage===k?'active':''}">${l}</span>`;
}).join('<i>→</i>');
}
function init() {
const sel = $('exampleSelect');
examples.forEach((e,i) => { sel.innerHTML += `<option value="${i}">${e.label}</option>`; });
$('inputArea').value = 'n=5';
buildSteps(examples[0].input);
stepCtrl = new StepController({onStep: render});
stepCtrl.setSteps(steps.map((_,i)=>i));
$('stepInfo').textContent = '步骤 1 / ' + steps.length;
stepCtrl.onStep = (idx) => { render(idx); $('stepInfo').textContent = `步骤 ${idx+1} / ${steps.length}`; };
$('applyBtn').onclick = () => {
const m = $('inputArea').value.match(/n=(\d+)/);
if (!m) { alert('格式: n=5'); return; }
buildSteps(parseInt(m[1]));
stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length;
};
$('exampleSelect').onchange = () => {
const e = examples[parseInt($('exampleSelect').value)];
$('inputArea').value = `n=${e.input}`;
buildSteps(e.input); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0);
};
$('prevBtn').onclick = () => stepCtrl.prev();
$('nextBtn').onclick = () => stepCtrl.next();
$('jumpBtn').onclick = () => stepCtrl.jumpToEnd();
$('autoBtn').onclick = () => { const on = stepCtrl.toggleAuto(); $('autoBtn').textContent = on ? '暂停' : '自动播放'; };
$('resetBtn').onclick = () => { stepCtrl.reset(); $('autoBtn').textContent = '自动播放'; };
}
init();
$('codeArea').innerHTML = renderCode(`def climbStairs(n):
if n <= 2: return n
dp = [0] * (n + 1)
dp[1], dp[2] = 1, 2
for i in range(3, n + 1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]`, {lang:'Python'});
})();
</script>
</body>
</html>