176 lines
6.7 KiB
HTML
176 lines
6.7 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>003. 最长连续序列 – 图解</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>🟡 003. 最长连续序列 <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: [100,4,200,1,3,2], label: '示例1: [100,4,200,1,3,2]'},
|
||
{input: [0,3,7,2,5,8,4,6,0,1], label: '示例2: [0,3,7,2,5,8,4,6,0,1]'},
|
||
];
|
||
|
||
let nums, steps, stepCtrl;
|
||
|
||
function buildSteps(arr) {
|
||
nums = arr; steps = [];
|
||
const numSet = new Set(arr);
|
||
const visited = new Set();
|
||
let best = 0;
|
||
|
||
steps.push({stage:'start', msg:'将所有数放入集合,然后寻找每个连续序列的起点', sorted:[...arr].sort((a,b)=>a-b), current:-1, streak:0, best:0});
|
||
const sortedUnique = [...new Set(arr)].sort((a,b)=>a-b);
|
||
|
||
for (const num of sortedUnique) {
|
||
if (numSet.has(num - 1)) continue; // not a start
|
||
let streak = 1;
|
||
let current = num;
|
||
const streakNums = [num];
|
||
steps.push({stage:'start_seq', msg:`${num} 是连续序列的起点(${num-1} 不在集合中)`, sorted:sortedUnique, current:num, streakNums, streak, best});
|
||
while (numSet.has(current + 1)) {
|
||
current++;
|
||
streak++;
|
||
streakNums.push(current);
|
||
steps.push({stage:'extend', msg:`序列延伸:${current - 1} + 1 = ${current} 在集合中`, sorted:sortedUnique, current, streakNums, streak, best});
|
||
}
|
||
best = Math.max(best, streak);
|
||
steps.push({stage:'end_seq', msg:`序列结束,长度 = ${streak},当前最长 = ${best}`, sorted:sortedUnique, current, streakNums, streak, best});
|
||
}
|
||
steps.push({stage:'done', msg:`最长连续序列长度为 ${best}`, sorted:sortedUnique, current:-1, streakNums:[], streak:0, best});
|
||
}
|
||
|
||
function render(step) {
|
||
const s = steps[step];
|
||
const hl = {};
|
||
if (s.streakNums) s.streakNums.forEach(n => { hl[n] = s.stage==='end_seq'?'green':'purple'; });
|
||
if (s.current >= 0) hl[s.current] = 'orange';
|
||
let viz = '<div style="margin-bottom:8px;"><b>排序去重后:</b></div>';
|
||
viz += renderArray(s.sorted, {highlights: hl});
|
||
if (s.streakNums && s.streakNums.length > 0) {
|
||
viz += '<div style="margin-top:8px;color:#8b5cf6;">连续序列: [' + s.streakNums.join(', ') + ']' + ' 长度=' + s.streak + '</div>';
|
||
}
|
||
$('vizArea').innerHTML = viz;
|
||
|
||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||
if (s.best > 0) $('detailContent').innerHTML += `<div class="current-answer">当前最长连续序列长度:<b>${s.best}</b></div>`;
|
||
|
||
if (s.stage === 'done') {
|
||
$('resultContent').innerHTML = `<div class="final-answer">最长连续序列长度 = <b>${s.best}</b></div>`;
|
||
}
|
||
|
||
$('hintText').textContent = s.msg;
|
||
const stages = ['start→开始','start_seq→起点','extend→延伸','end_seq→结束','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 = '[100,4,200,1,3,2]';
|
||
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 longestConsecutive(nums):
|
||
num_set = set(nums)
|
||
longest = 0
|
||
for num in num_set:
|
||
if num - 1 not in num_set:
|
||
streak = 1
|
||
while num + streak in num_set:
|
||
streak += 1
|
||
longest = max(longest, streak)
|
||
return longest`, {lang:'Python'});
|
||
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html> |