This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>061. 分割回文串 – 图解</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>🟡 061. 分割回文串 <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: "aab", label: '示例1: "aab"'},
|
||||
{input: "a", label: '示例2: "a"'},
|
||||
{input: "racecar", label: '示例3: "racecar"'},
|
||||
];
|
||||
let s, steps, stepCtrl;
|
||||
|
||||
function isPalindrome(str, l, r) {
|
||||
while (l < r) { if (str[l] !== str[r]) return false; l++; r--; }
|
||||
return true;
|
||||
}
|
||||
|
||||
function buildSteps(str) {
|
||||
s = str; steps = [];
|
||||
const result = [];
|
||||
const path = [];
|
||||
|
||||
steps.push({stage:'start', msg:`对 "${str}" 进行回文分割`, path:[], start:0, checking:null, result:[]});
|
||||
|
||||
function backtrack(start) {
|
||||
if (start === s.length) {
|
||||
result.push([...path]);
|
||||
steps.push({stage:'collect', msg:`分割完成: [${path.map(p=>'"'+p+'"').join(', ')}]`, path:[...path], start, checking:null, result:JSON.parse(JSON.stringify(result))});
|
||||
return;
|
||||
}
|
||||
for (let end = start; end < s.length; end++) {
|
||||
const sub = s.substring(start, end + 1);
|
||||
const isPalin = isPalindrome(s, start, end);
|
||||
steps.push({stage:'check', msg:`检查 "${sub}" (${start}..${end}): ${isPalin?'是回文 ✓':'不是回文 ✗'}`, path:[...path], start, checking:{from:start, to:end, isPalin}, result:JSON.parse(JSON.stringify(result))});
|
||||
if (isPalin) {
|
||||
path.push(sub);
|
||||
steps.push({stage:'choose', msg:`选择 "${sub}" 加入路径`, path:[...path], start:end+1, checking:null, result:JSON.parse(JSON.stringify(result))});
|
||||
backtrack(end + 1);
|
||||
path.pop();
|
||||
steps.push({stage:'undo', msg:`回溯:移除 "${sub}"`, path:[...path], start, checking:null, result:JSON.parse(JSON.stringify(result))});
|
||||
}
|
||||
}
|
||||
}
|
||||
backtrack(0);
|
||||
steps.push({stage:'done', msg:`共 ${result.length} 种分割方案`, path:[], start:-1, checking:null, result:JSON.parse(JSON.stringify(result))});
|
||||
}
|
||||
|
||||
function render(step) {
|
||||
const st = steps[step];
|
||||
let viz = `<div style="font-size:20px;font-family:monospace;letter-spacing:2px;margin:12px 0;">`;
|
||||
// color the string by current path partitions
|
||||
let pos = 0;
|
||||
const parts = st.path;
|
||||
const colors = ['#dbeafe','#dcfce7','#ede9fe','#fff7ed','#cffafe','#fee2e2'];
|
||||
for (let pi=0; pi<parts.length; pi++) {
|
||||
const part = parts[pi];
|
||||
for (let j=0; j<part.length; j++) {
|
||||
viz += `<span style="background:${colors[pi%colors.length]};padding:2px 1px;border-radius:3px;">${part[j]}</span>`;
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
// remaining chars
|
||||
for (let i=pos; i<s.length; i++) {
|
||||
const isChecking = st.checking && i>=st.checking.from && i<=st.checking.to;
|
||||
viz += `<span style="${isChecking?(st.checking.isPalin?'background:#dcfce7;':'background:#fee2e2;'):''}padding:2px 1px;border-radius:3px;">${s[i]}</span>`;
|
||||
}
|
||||
viz += '</div>';
|
||||
// path
|
||||
viz += `<div style="margin-top:8px;"><b>当前分割:</b>`;
|
||||
if (st.path.length > 0) {
|
||||
st.path.forEach((p,i) => { viz += `<span class="chip" style="min-width:auto;padding:2px 8px;background:${colors[i%colors.length]};font-size:12px;">"${p}"</span>`; });
|
||||
} else viz += '<span style="color:var(--text-muted);">空</span>';
|
||||
viz += '</div>';
|
||||
if (st.result.length > 0) {
|
||||
viz += '<div style="margin-top:8px;"><b>已找到:</b></div><div style="display:flex;flex-wrap:wrap;gap:4px;">';
|
||||
st.result.forEach(r => { viz += `<span class="chip green" style="min-width:auto;padding:2px 8px;font-size:11px;">[${r.map(x=>'"'+x+'"').join(',')}]</span>`; });
|
||||
viz += '</div>';
|
||||
}
|
||||
$('vizArea').innerHTML = viz;
|
||||
$('detailContent').innerHTML = '<div class="calc-block">' + st.msg + '</div>';
|
||||
if (st.stage==='done') $('resultContent').innerHTML = `<div class="final-answer">共 <b>${st.result.length}</b> 种分割方案</div>`;
|
||||
$('hintText').textContent = st.msg;
|
||||
const stages = ['start→开始','check→检查回文','choose→选择','collect→收集','undo→回溯','done→完成'];
|
||||
$('pipeline').innerHTML = stages.map(stt => { const [k,l]=stt.split('→'); return `<span class="pipe-step ${st.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 = '"aab"';
|
||||
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 = $('inputArea').value.replace(/"/g,'').trim();
|
||||
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 partition(s):
|
||||
res = []
|
||||
def is_palindrome(sub, l, r):
|
||||
while l < r:
|
||||
if sub[l] != sub[r]: return False
|
||||
l += 1; r -= 1
|
||||
return True
|
||||
def backtrack(start, path):
|
||||
if start == len(s):
|
||||
res.append(path[:])
|
||||
return
|
||||
for end in range(start, len(s)):
|
||||
if is_palindrome(s, start, end):
|
||||
path.append(s[start:end+1])
|
||||
backtrack(end + 1, path)
|
||||
path.pop()
|
||||
backtrack(0, [])
|
||||
return res`, {lang:'Python'});
|
||||
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user