This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>004. 移动零 – 图解</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>🟢 004. 移动零 <span class="badge easy">简单</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: [0,1,0,3,12], label: '示例1: [0,1,0,3,12]'},
|
||||
{input: [0], label: '示例2: [0]'},
|
||||
{input: [1,0,2,0,3], label: '示例3: [1,0,2,0,3]'},
|
||||
];
|
||||
let nums, steps, stepCtrl;
|
||||
|
||||
function buildSteps(arr) {
|
||||
nums = [...arr]; steps = [];
|
||||
let slow = 0;
|
||||
steps.push({stage:'start', msg:'用快慢指针:slow 指向第一个可放非零元素的位置,fast 遍历数组', arr:[...nums], slow:0, fast:0});
|
||||
for (let fast = 0; fast < nums.length; fast++) {
|
||||
steps.push({stage:'check', msg:`fast=${fast},nums[${fast}]=${nums[fast]} ${nums[fast]===0?'是零,跳过':'不是零,交换到前面'}`, arr:[...nums], slow, fast});
|
||||
if (nums[fast] !== 0) {
|
||||
if (slow !== fast) {
|
||||
[nums[slow], nums[fast]] = [nums[fast], nums[slow]];
|
||||
steps.push({stage:'swap', msg:`交换 nums[${slow}]=0 和 nums[${fast}]=${nums[slow]} → [${nums}]`, arr:[...nums], slow, fast});
|
||||
}
|
||||
slow++;
|
||||
steps.push({stage:'advance', msg:`slow 前进到 ${slow}`, arr:[...nums], slow, fast});
|
||||
}
|
||||
}
|
||||
steps.push({stage:'done', msg:`完成!所有非零元素已移到前面`, arr:[...nums], slow, fast:nums.length});
|
||||
}
|
||||
|
||||
function render(step) {
|
||||
const s = steps[step];
|
||||
const hl = {};
|
||||
if (s.fast < s.arr.length) hl[s.fast] = 'orange';
|
||||
if (s.slow < s.arr.length) hl[s.slow] = 'blue';
|
||||
// mark placed non-zeros
|
||||
for (let i = 0; i < s.slow; i++) { if (s.arr[i] !== 0) hl[i] = 'green'; }
|
||||
let viz = renderArray(s.arr, {highlights: hl, pointers: {slow: s.slow, fast: s.fast}});
|
||||
$('vizArea').innerHTML = viz;
|
||||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||||
if (s.stage === 'done') {
|
||||
$('resultContent').innerHTML = `<div class="final-answer">结果:<b>[${s.arr}]</b></div>`;
|
||||
}
|
||||
$('hintText').textContent = s.msg;
|
||||
const stages = ['start→开始','check→检查','swap→交换','advance→前进','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 = '[0,1,0,3,12]';
|
||||
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 moveZeroes(nums):
|
||||
slow = 0
|
||||
for fast in range(len(nums)):
|
||||
if nums[fast] != 0:
|
||||
nums[slow], nums[fast] = nums[fast], nums[slow]
|
||||
slow += 1`, {lang:'Python'});
|
||||
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user