This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>002. 字母异位词分组 – 图解</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>🟡 002. 字母异位词分组 <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: ["eat","tea","tan","ate","nat","bat"], label: '示例1'},
|
||||
{input: [""], label: '示例2: [""]'},
|
||||
{input: ["a"], label: '示例3: ["a"]'},
|
||||
];
|
||||
|
||||
let strs, steps, stepCtrl;
|
||||
|
||||
function sortedKey(s) { return s.split('').sort().join(''); }
|
||||
|
||||
function buildSteps(arr) {
|
||||
strs = arr; steps = [];
|
||||
const groups = {};
|
||||
steps.push({stage:'start', msg:'遍历每个字符串,将其按字母排序后的结果作为 key 分组', current:'', key:'', groups:{}});
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
const s = arr[i];
|
||||
const key = sortedKey(s);
|
||||
steps.push({stage:'sort', msg:`处理 "${s}":排序后 key = "${key}"`, current:s, key, groups:JSON.parse(JSON.stringify(groups)), idx:i});
|
||||
if (!groups[key]) groups[key] = [];
|
||||
groups[key].push(s);
|
||||
steps.push({stage:'group', msg:`将 "${s}" 加入分组 ["${key}"]`, current:s, key, groups:JSON.parse(JSON.stringify(groups)), idx:i});
|
||||
}
|
||||
steps.push({stage:'done', msg:'分组完成', current:'', key:'', groups:JSON.parse(JSON.stringify(groups))});
|
||||
}
|
||||
|
||||
function render(step) {
|
||||
const s = steps[step];
|
||||
// viz
|
||||
let viz = '<div class="nums-line">';
|
||||
strs.forEach((st,i) => {
|
||||
const cls = s.idx===i ? (s.stage==='group'?'green':'orange') : 'default';
|
||||
viz += `<span class="chip ${cls}" style="min-width:auto;padding:4px 12px;">"${st}"</span>`;
|
||||
});
|
||||
viz += '</div>';
|
||||
if (s.current) viz += `<div style="margin-top:8px;">当前: <code>"${s.current}"</code> → key: <code>"${s.key}"</code></div>`;
|
||||
$('vizArea').innerHTML = viz;
|
||||
|
||||
let detail = '<div class="calc-block">' + s.msg + '</div>';
|
||||
detail += '<div style="margin-top:8px;"><b>分组结果:</b></div>';
|
||||
Object.entries(s.groups).forEach(([k,v]) => {
|
||||
detail += `<div style="margin:4px 0;"><code>${k}</code> → [${v.map(x=>`"${x}"`).join(', ')}]</div>`;
|
||||
});
|
||||
$('detailContent').innerHTML = detail;
|
||||
|
||||
if (s.stage === 'done') {
|
||||
const result = Object.values(s.groups);
|
||||
$('resultContent').innerHTML = `<div class="final-answer">返回 <b>${JSON.stringify(result)}</b><br>共 ${result.length} 个分组</div>`;
|
||||
}
|
||||
|
||||
$('hintText').textContent = s.msg;
|
||||
const stages = ['start→开始','sort→排序','group→分组','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 = '["eat","tea","tan","ate","nat","bat"]';
|
||||
|
||||
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 = () => {
|
||||
try {
|
||||
const arr = JSON.parse($('inputArea').value);
|
||||
buildSteps(arr);
|
||||
stepCtrl.setSteps(steps.map((_,i)=>i));
|
||||
render(0);
|
||||
} catch(e) { alert('请输入合法 JSON 数组,如 ["eat","tea"]'); }
|
||||
};
|
||||
$('exampleSelect').onchange = () => {
|
||||
const e = examples[parseInt($('exampleSelect').value)];
|
||||
$('inputArea').value = JSON.stringify(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 groupAnagrams(strs):
|
||||
groups = {}
|
||||
for s in strs:
|
||||
key = ''.join(sorted(s))
|
||||
if key not in groups:
|
||||
groups[key] = []
|
||||
groups[key].append(s)
|
||||
return list(groups.values())`, {lang:'Python'});
|
||||
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user