This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>059. 括号生成 – 图解</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>🟡 059. 括号生成 <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 = [
|
||||
{input: 3, label: '示例1: n=3'},
|
||||
{input: 1, label: '示例2: n=1'},
|
||||
{input: 2, label: '示例3: n=2'},
|
||||
];
|
||||
let n, steps, stepCtrl;
|
||||
|
||||
function buildSteps(nn) {
|
||||
n = nn; steps = [];
|
||||
const result = [];
|
||||
const path = [];
|
||||
|
||||
steps.push({stage:'start', msg:`生成 ${n} 对括号的有效组合`, path:[], open:0, close:0, result:[]});
|
||||
|
||||
function backtrack(open, close) {
|
||||
if (path.length === 2 * n) {
|
||||
result.push(path.join(''));
|
||||
steps.push({stage:'collect', msg:`组合完成: "${path.join('')}"`, path:[...path], open, close, result:JSON.parse(JSON.stringify(result))});
|
||||
return;
|
||||
}
|
||||
if (open < n) {
|
||||
path.push('(');
|
||||
steps.push({stage:'add_open', msg:`open(${open})<${n},添加 '(' → open=${open+1}`, path:[...path], open:open+1, close, result:JSON.parse(JSON.stringify(result))});
|
||||
backtrack(open + 1, close);
|
||||
path.pop();
|
||||
steps.push({stage:'undo', msg:`回溯 '(',open=${open}`, path:[...path], open, close, result:JSON.parse(JSON.stringify(result))});
|
||||
}
|
||||
if (close < open) {
|
||||
path.push(')');
|
||||
steps.push({stage:'add_close', msg:`close(${close})<open(${open}),添加 ')' → close=${close+1}`, path:[...path], open, close:close+1, result:JSON.parse(JSON.stringify(result))});
|
||||
backtrack(open, close + 1);
|
||||
path.pop();
|
||||
steps.push({stage:'undo', msg:`回溯 ')',close=${close}`, path:[...path], open, close, result:JSON.parse(JSON.stringify(result))});
|
||||
}
|
||||
}
|
||||
backtrack(0, 0);
|
||||
steps.push({stage:'done', msg:`共 ${result.length} 个有效组合`, path:[], open:0, close:0, result:JSON.parse(JSON.stringify(result))});
|
||||
}
|
||||
|
||||
function render(step) {
|
||||
const s = steps[step];
|
||||
let viz = `<div style="font-size:28px;font-weight:700;letter-spacing:4px;margin:12px 0;font-family:monospace;">`;
|
||||
const str = s.path.join('');
|
||||
for (let i=0; i<str.length; i++) {
|
||||
const ch = str[i];
|
||||
viz += `<span style="color:${ch==='('?'var(--blue)':'var(--green)'};">${ch}</span>`;
|
||||
}
|
||||
viz += '<span style="color:var(--text-muted);">_'.repeat(Math.max(0, 2*n - str.length)) + '</span></div>';
|
||||
viz += `<div style="display:flex;gap:16px;margin-top:8px;">
|
||||
<span style="color:var(--blue);font-weight:700;">open = ${s.open} / ${n}</span>
|
||||
<span style="color:var(--green);font-weight:700;">close = ${s.close} / ${s.open}</span>
|
||||
</div>`;
|
||||
// visual bar
|
||||
viz += `<div style="margin-top:8px;display:flex;gap:4px;">`;
|
||||
for (let i=0; i<n; i++) viz += `<div style="width:24px;height:8px;border-radius:4px;background:${i<s.open?'var(--blue)':'#e2e8f0'};"></div>`;
|
||||
viz += `<span style="margin:0 8px;font-size:14px;">(</span>`;
|
||||
for (let i=0; i<n; i++) viz += `<div style="width:24px;height:8px;border-radius:4px;background:${i<s.close?'var(--green)':'#e2e8f0'};"></div>`;
|
||||
viz += `<span style="margin-left:8px;font-size:14px;">)</span></div>`;
|
||||
if (s.result.length > 0) {
|
||||
viz += '<div style="margin-top:8px;"><b>已找到:</b></div><div style="display:flex;flex-wrap:wrap;gap:4px;">';
|
||||
s.result.forEach(r => { viz += `<span class="chip green" style="min-width:auto;padding:2px 10px;font-size:13px;font-family:monospace;">${r}</span>`; });
|
||||
viz += '</div>';
|
||||
}
|
||||
$('vizArea').innerHTML = viz;
|
||||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||||
if (s.stage==='done') $('resultContent').innerHTML = `<div class="final-answer">共 <b>${s.result.length}</b> 个组合<br>[${s.result.map(r=>'"'+r+'"').join(', ')}]</div>`;
|
||||
$('hintText').textContent = s.msg;
|
||||
const stages = ['start→开始','add_open→加左括号','add_close→加右括号','collect→收集','undo→回溯','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 = '3';
|
||||
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 v = parseInt($('inputArea').value);
|
||||
if (isNaN(v)||v<1||v>6) { alert('请输入1-6的整数'); return; }
|
||||
buildSteps(v); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length;
|
||||
};
|
||||
$('exampleSelect').onchange = () => {
|
||||
const e = examples[parseInt($('exampleSelect').value)];
|
||||
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 generateParenthesis(n):
|
||||
res = []
|
||||
def backtrack(path, open, close):
|
||||
if len(path) == 2 * n:
|
||||
res.append(''.join(path))
|
||||
return
|
||||
if open < n:
|
||||
path.append('(')
|
||||
backtrack(path, open + 1, close)
|
||||
path.pop()
|
||||
if close < open:
|
||||
path.append(')')
|
||||
backtrack(path, open, close + 1)
|
||||
path.pop()
|
||||
backtrack([], 0, 0)
|
||||
return res`, {lang:'Python'});
|
||||
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user