174 lines
6.8 KiB
HTML
174 lines
6.8 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-Hans">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>005. 盛最多水的容器 – 图解</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>🟡 005. 盛最多水的容器 <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: [1,8,6,2,5,4,8,3,7], label: '示例1: [1,8,6,2,5,4,8,3,7]'},
|
||
{input: [1,1], label: '示例2: [1,1]'},
|
||
];
|
||
let height, steps, stepCtrl;
|
||
|
||
function buildSteps(arr) {
|
||
height = [...arr]; steps = [];
|
||
let left = 0, right = arr.length - 1, maxArea = 0;
|
||
steps.push({stage:'init', msg:'双指针从两端开始,计算面积并移动较矮的一端', left, right, maxArea, area:0});
|
||
while (left < right) {
|
||
const h = Math.min(arr[left], arr[right]);
|
||
const w = right - left;
|
||
const area = h * w;
|
||
maxArea = Math.max(maxArea, area);
|
||
steps.push({stage:'calc', msg:`left=${left}(h=${arr[left]}) right=${right}(h=${arr[right]}) → 高度=${h} 宽度=${w} 面积=${area}`, left, right, maxArea, area});
|
||
if (arr[left] < arr[right]) {
|
||
steps.push({stage:'move', msg:`左边 ${arr[left]} < 右边 ${arr[right]},移动左指针 → ${left+1}`, left:left+1, right, maxArea, area});
|
||
left++;
|
||
} else {
|
||
steps.push({stage:'move', msg:`右边 ${arr[right]} ≤ 左边 ${arr[left]},移动右指针 → ${right-1}`, left, right:right-1, maxArea, area});
|
||
right--;
|
||
}
|
||
}
|
||
steps.push({stage:'done', msg:`左右指针相遇,最大面积为 ${maxArea}`, left, right, maxArea, area:0});
|
||
}
|
||
|
||
function render(step) {
|
||
const s = steps[step];
|
||
const hl = {};
|
||
if (s.left !== undefined) hl[s.left] = 'blue';
|
||
if (s.right !== undefined) hl[s.right] = 'blue';
|
||
for (let i = s.left+1; i < s.right; i++) hl[i] = 'default';
|
||
let viz = renderArray(height, {highlights: hl, pointers: {L: s.left, R: s.right}});
|
||
// draw bars
|
||
viz += '<div style="display:flex;align-items:flex-end;gap:4px;height:120px;margin-top:12px;padding:8px 0;">';
|
||
const maxH = Math.max(...height);
|
||
height.forEach((h,i) => {
|
||
const pct = (h / maxH * 100);
|
||
const bg = i===s.left || i===s.right ? 'var(--blue)' : '#cbd5e1';
|
||
viz += `<div style="width:36px;height:${pct}%;background:${bg};border-radius:4px 4px 0 0;display:flex;align-items:flex-start;justify-content:center;font-size:11px;font-weight:700;color:${i===s.left||i===s.right?'white':'#475569'};padding-top:4px;">${h}</div>`;
|
||
});
|
||
viz += '</div>';
|
||
$('vizArea').innerHTML = viz;
|
||
|
||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||
if (s.maxArea > 0) $('detailContent').innerHTML += `<div class="current-answer">最大面积:<b>${s.maxArea}</b></div>`;
|
||
if (s.stage === 'done') {
|
||
$('resultContent').innerHTML = `<div class="final-answer">最大盛水量 = <b>${s.maxArea}</b></div>`;
|
||
}
|
||
$('hintText').textContent = s.msg;
|
||
const stages = ['init→初始化','calc→计算','move→移动','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 = '[1,8,6,2,5,4,8,3,7]';
|
||
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 数组'); }
|
||
};
|
||
$('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 maxArea(height):
|
||
left, right = 0, len(height) - 1
|
||
max_area = 0
|
||
while left < right:
|
||
h = min(height[left], height[right])
|
||
w = right - left
|
||
max_area = max(max_area, h * w)
|
||
if height[left] < height[right]:
|
||
left += 1
|
||
else:
|
||
right -= 1
|
||
return max_area`, {lang:'Python'});
|
||
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html> |