feat: LeetCode Hot 100 - 100道题完整交互式图解页面
Deploy / deploy (push) Successful in 7s

This commit is contained in:
2026-08-24 04:35:13 +00:00
parent e4fe5afb80
commit 4f830ad352
114 changed files with 32137 additions and 82 deletions
+198
View File
@@ -0,0 +1,198 @@
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>053. 课程表 – 图解</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>🟡 053. 课程表 <span class="badge medium">中等</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 = [
{numCourses: 4, prerequisites: [[1,0],[2,0],[3,1],[3,2]], label: '示例1: 4门课, [1,0],[2,0],[3,1],[3,2]'},
{numCourses: 2, prerequisites: [[1,0],[0,1]], label: '示例2: 2门课, 有环'},
{numCourses: 3, prerequisites: [[1,0],[2,1]], label: '示例3: 3门课, 无环'},
];
let N, edges, steps, stepCtrl, adj, stateMap;
function buildSteps(n, prereq) {
N = n; edges = prereq; steps = [];
adj = Array.from({length:n}, ()=>[]);
for (const [a,b] of prereq) adj[a].push(b);
stateMap = new Array(n).fill(0); // 0=未访问, 1=进行中, 2=完成
steps.push({stage:'init', msg:`DFS环检测: ${n}门课程, ${prereq.length}条依赖`, node:-1, state:[...stateMap], topo:[], hasCycle:false});
let hasCycle = false;
const topoOrder = [];
function dfs(node) {
stateMap[node] = 1;
steps.push({stage:'visiting', msg:`访问课程 ${node},标记为"进行中"`, node, state:[...stateMap], topo:[...topoOrder], hasCycle:false});
for (const nb of adj[node]) {
if (stateMap[nb] === 1) {
hasCycle = true;
steps.push({stage:'cycle', msg:`课程 ${nb} 正在访问中!检测到环 ${node}→${nb}`, node:nb, state:[...stateMap], topo:[...topoOrder], hasCycle:true});
return true;
}
if (stateMap[nb] === 0) {
if (dfs(nb)) return true;
}
}
stateMap[node] = 2;
topoOrder.push(node);
steps.push({stage:'done', msg:`课程 ${node} 完成,加入拓扑序列`, node, state:[...stateMap], topo:[...topoOrder], hasCycle:false});
return false;
}
for (let i=0; i<n; i++) {
if (stateMap[i] === 0) {
if (dfs(i)) break;
}
}
if (hasCycle) steps.push({stage:'result', msg:'检测到环,无法完成所有课程', node:-1, state:[...stateMap], topo:[...topoOrder], hasCycle:true});
else steps.push({stage:'result', msg:'无环,可以完成所有课程!拓扑序: [' + topoOrder.reverse().join(', ') + ']', node:-1, state:[...stateMap], topo:[...topoOrder], hasCycle:false});
}
function render(step) {
const s = steps[step];
const stateColors = {0:'#e2e8f0', 1:'#fef3c7', 2:'#dcfce7'};
const stateText = {0:'未访问', 1:'进行中', 2:'已完成'};
const stateBorder = {0:'#94a3b8', 1:'#f59e0b', 2:'#16a34a'};
let viz = '<div style="display:flex;gap:16px;flex-wrap:wrap;margin-bottom:12px;">';
for (let i=0; i<N; i++) {
const bg = stateColors[s.state[i]];
const border = stateBorder[s.state[i]];
const isCurr = i===s.node;
viz += `<div style="width:60px;height:60px;border-radius:50%;display:flex;flex-direction:column;align-items:center;justify-content:center;background:${bg};border:3px solid ${border};font-weight:700;transition:all 0.3s;${isCurr?'transform:scale(1.15);box-shadow:0 0 0 4px rgba(59,130,246,0.3);':''}">${i}<span style="font-size:10px;font-weight:400;">${stateText[s.state[i]]}</span></div>`;
}
viz += '</div>';
viz += '<div style="margin-top:8px;"><b>依赖关系:</b></div>';
for (const [a,b] of edges) viz += `<span style="margin-right:12px;"><code>${a}←${b}</code></span>`;
if (s.topo.length > 0) viz += `<div style="margin-top:8px;"><b>拓扑序:</b>${s.topo.join(' → ')}</div>`;
$('vizArea').innerHTML = viz;
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
if (s.stage==='result') {
if (s.hasCycle) $('resultContent').innerHTML = '<div class="final-answer" style="border-color:#f87171;background:#fef2f2;">返回 <b>False</b>(检测到环,无法完成)</div>';
else $('resultContent').innerHTML = `<div class="final-answer">返回 <b>True</b>(可以完成所有课程)<br>拓扑序: ${s.topo.join(' → ')}</div>`;
}
$('hintText').textContent = s.msg;
const stages = ['init→初始化','visiting→访问中','done→完成','cycle→检测到环','result→结果'];
$('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 = '4, [[1,0],[2,0],[3,1],[3,2]]';
buildSteps(examples[0].numCourses, examples[0].prerequisites);
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 = () => {
try {
const m = $('inputArea').value.match(/(\d+),\s*(\[[\s\S]*\])/);
if (!m) { alert('格式: numCourses, [[1,0],[2,0]]'); return; }
buildSteps(parseInt(m[1]), JSON.parse(m[2]));
stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length;
} catch(e) { alert('输入格式错误'); }
};
$('exampleSelect').onchange = () => {
const e = examples[parseInt($('exampleSelect').value)];
buildSteps(e.numCourses, e.prerequisites); 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 canFinish(numCourses, prerequisites):
adj = [[] for _ in range(numCourses)]
for a, b in prerequisites:
adj[a].append(b)
state = [0] * numCourses # 0=未访问 1=进行中 2=完成
def dfs(node):
state[node] = 1
for nb in adj[node]:
if state[nb] == 1:
return True # 环
if state[nb] == 0 and dfs(nb):
return True
state[node] = 2
return False
for i in range(numCourses):
if state[i] == 0 and dfs(i):
return False
return True`, {lang:'Python'});
})();
</script>
</body>
</html>