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
+241
View File
@@ -0,0 +1,241 @@
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>071. 字符串解码 – 图解</title>
<link rel="stylesheet" href="../shared/style.css">
<style>
.vis-area { min-height: 120px; padding: 16px 0; }
.code-section { margin-top: 16px; }
.char-box { display:inline-flex; align-items:center; justify-content:center; min-width:28px; height:32px; border-radius:6px; font-weight:700; font-size:14px; margin:1px; padding:0 4px; transition:all 0.25s; }
.char-box.default { background:#e2e8f0; color:var(--text); }
.char-box.digit { background:#dbeafe; color:#1e40af; }
.char-box.current { background:#fef3c7; color:#92400e; box-shadow:0 0 0 2px rgba(245,158,11,0.3); }
.char-box.open { background:#ede9fe; color:#5b21b6; }
.char-box.close { background:#ede9fe; color:#5b21b6; }
.char-box.decoded { background:#dcfce7; color:#166534; }
.dual-stack { display:flex; gap:16px; flex-wrap:wrap; }
.dual-stack > div { flex:1; min-width:140px; }
.decode-result { margin-top:10px; padding:10px 14px; background:#f0fdf4; border-radius:8px; font-family:monospace; font-size:14px; word-break:break-all; }
</style>
</head>
<body>
<div class="container">
<h1>🟡 071. 字符串解码 <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='s="3[a2[c]]"'>
<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() {
const examples = [
{s:'3[a2[c]]', label:'示例1: 3[a2[c]]'},
{s:'3[a]2[bc]', label:'示例2: 3[a]2[bc]'},
{s:'2[abc]3[cd]ef', label:'示例3: 2[abc]3[cd]ef'},
{s:'10[a]', label:'示例4: 10[a]'},
];
let sStr, steps, stepCtrl;
function buildSteps(s) {
sStr = s;
steps = [];
const countStack = [];
const strStack = [];
let currentStr = '';
let num = 0;
steps.push({stage:'init', msg:'初始化:数字栈、字符串栈均为空,当前字符串为空', idx:-1, countStack:[], strStack:[], currentStr:'', num:0, charStatus:{}, decodedStr:''});
for (let i = 0; i < s.length; i++) {
const c = s[i];
const charStatus = {};
// Mark previous as decoded
for (let j = 0; j < i; j++) charStatus[j] = 'decoded';
charStatus[i] = 'current';
if (c >= '0' && c <= '9') {
num = num * 10 + parseInt(c);
steps.push({stage:'digit', msg:`'${c}' 是数字,累计 num = ${num}`, idx:i, countStack:[...countStack], strStack:[...strStack], currentStr, num, charStatus, decodedStr:currentStr});
} else if (c === '[') {
countStack.push(num);
strStack.push(currentStr);
steps.push({stage:'open', msg:`遇到 '[',将 num=${num} 入数字栈,"${currentStr}" 入字符串栈,重置`, idx:i, countStack:[...countStack], strStack:[...strStack], currentStr:'', num:0, charStatus, decodedStr:strStack[strStack.length-1]||''});
currentStr = '';
num = 0;
} else if (c === ']') {
const repeatCount = countStack.pop();
const prevStr = strStack.pop();
const decoded = prevStr + currentStr.repeat(repeatCount);
steps.push({stage:'close', msg:`遇到 ']',弹出数字 ${repeatCount},弹出字符串 "${prevStr}",解码:"${prevStr}" + "${currentStr}"×${repeatCount} = "${decoded}"`, idx:i, countStack:[...countStack], strStack:[...strStack], currentStr:decoded, num:0, charStatus, decodedStr:decoded});
currentStr = decoded;
} else {
currentStr += c;
steps.push({stage:'letter', msg:`'${c}' 是字母,拼接到当前字符串 "${currentStr}"`, idx:i, countStack:[...countStack], strStack:[...strStack], currentStr, num, charStatus, decodedStr:currentStr});
}
}
steps.push({stage:'done', msg:`解码完成:${currentStr}`, idx:s.length, countStack:[], strStack:[], currentStr, num:0, charStatus:{}, decodedStr:currentStr});
}
function render(step) {
const s = steps[step];
// String display
let viz = '<div style="margin-bottom:10px;"><b>输入字符串:</b><div style="display:flex;flex-wrap:wrap;">';
for (let i = 0; i < sStr.length; i++) {
const c = sStr[i];
let cls = s.charStatus[i] || 'default';
if (cls === 'current') {
if (c >= '0' && c <= '9') cls = 'digit current';
else if (c === '[') cls = 'open current';
else if (c === ']') cls = 'close current';
else cls = 'current';
} else if (cls === 'decoded') {
cls = 'decoded';
} else if (c >= '0' && c <= '9') {
cls = 'digit';
} else if (c === '[' || c === ']') {
cls = 'open';
}
viz += `<span class="char-box ${cls}">${c}</span>`;
}
viz += '</div></div>';
// Stacks
viz += '<div class="dual-stack">';
viz += '<div><b>数字栈</b>';
viz += renderStack(s.countStack.map(String), {topIndex: s.countStack.length - 1});
viz += '</div>';
viz += '<div><b>字符串栈</b>';
viz += renderStack(s.strStack.map(v => v.length > 12 ? v.substring(0,12)+'…' : v), {topIndex: s.strStack.length - 1});
viz += '</div>';
viz += '</div>';
viz += `<div style="margin-top:8px;font-size:13px;"><b>当前字符串:</b><code>${s.currentStr || '(空)'}</code></div>`;
if (s.num > 0) {
viz += `<div style="font-size:13px;color:var(--blue);"><b>累计数字:</b>${s.num}</div>`;
}
$('vizArea').innerHTML = viz;
let detail = `<div class="calc-block">${s.msg}</div>`;
detail += '<div style="margin-top:6px;font-size:12px;color:var(--text-secondary);">核心:遇到 [ 入栈保存状态,遇到 ] 出栈解码</div>';
$('detailContent').innerHTML = detail;
if (s.stage === 'done') {
$('resultContent').innerHTML = `<div class="final-answer">解码结果:<b>${s.decodedStr}</b><br>时间复杂度 O(n),空间 O(n)</div>`;
}
$('hintText').textContent = s.msg;
const stages = ['init→开始','digit→数字','open→入栈','letter→字母','close→解码','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 = 's="3[a2[c]]"';
buildSteps(examples[0].s);
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(/s="([^"]+)"/);
if (!m) { alert('格式: s="3[a2[c]]"'); return; }
buildSteps(m[1]);
stepCtrl.setSteps(steps.map((_,i)=>i)); render(0);
$('stepInfo').textContent = `步骤 1 / ${steps.length}`;
} catch(e) { alert('输入格式错误'); }
};
$('exampleSelect').onchange = () => {
const e = examples[parseInt($('exampleSelect').value)];
$('inputArea').value = `s="${e.s}"`;
buildSteps(e.s);
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 decodeString(s: str) -> str:
count_stack = []
str_stack = []
current = ''
num = 0
for c in s:
if c.isdigit():
num = num * 10 + int(c)
elif c == '[':
count_stack.append(num)
str_stack.append(current)
current = ''
num = 0
elif c == ']':
repeat = count_stack.pop()
prev = str_stack.pop()
current = prev + current * repeat
else:
current += c
return current`, {lang:'Python'});
})();
</script>
</body>
</html>