Files
2026-08-24 04:35:13 +00:00

189 lines
7.4 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>067. 寻找旋转排序数组中的最小值 – 图解</title>
<link rel="stylesheet" href="../shared/style.css">
<style>
.vis-area { min-height: 120px; padding: 16px 0; }
.code-section { margin-top: 16px; }
</style>
</head>
<body>
<div class="container">
<h1>🟡 067. 寻找旋转排序数组中的最小值 <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="nums=[3,4,5,1,2]">
<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() {
const examples = [
{nums:[3,4,5,1,2], label:'示例1: [3,4,5,1,2]'},
{nums:[4,5,6,7,0,1,2], label:'示例2: [4,5,6,7,0,1,2]'},
{nums:[11,13,15,17], label:'示例3: 未旋转 [11,13,15,17]'},
{nums:[2,1], label:'示例4: [2,1]'},
];
let numsArr, steps, stepCtrl;
function buildSteps(nums) {
numsArr = nums;
steps = [];
let left = 0, right = nums.length - 1;
steps.push({stage:'init', msg:`初始化:left=${left}, right=${right}`, left, right, mid:-1, minIdx:-1});
// If not rotated
if (nums[left] < nums[right]) {
steps.push({stage:'done', msg:`nums[${left}]=${nums[left]} < nums[${right}]=${nums[right]},数组未旋转,最小值为 ${nums[left]}`, left, right, mid:-1, minIdx:left});
return;
}
while (left < right) {
const mid = Math.floor((left + right) / 2);
steps.push({stage:'calc', msg:`mid = ⌊(${left}+${right})/2⌋ = ${mid},nums[${mid}] = ${nums[mid]},nums[${right}] = ${nums[right]}`, left, right, mid, minIdx:-1});
if (nums[mid] > nums[right]) {
steps.push({stage:'goRight', msg:`nums[${mid}]=${nums[mid]} > nums[${right}]=${nums[right]},最小值在 mid 右侧,left = ${mid}+1 = ${mid+1}`, left:mid+1, right, mid, minIdx:-1});
left = mid + 1;
} else {
steps.push({stage:'goLeft', msg:`nums[${mid}]=${nums[mid]} ≤ nums[${right}]=${nums[right]},最小值在 mid 或 mid 左侧,right = ${mid}`, left, right:mid, mid, minIdx:-1});
right = mid;
}
}
steps.push({stage:'done', msg:`left == right == ${left},最小值为 nums[${left}] = ${nums[left]}`, left, right, mid:-1, minIdx:left});
}
function render(step) {
const s = steps[step];
const arr = numsArr;
const hl = highlightRange(arr, s.left, s.right, 'cyan');
if (s.mid >= 0 && s.mid < arr.length) hl[s.mid] = 'orange';
if (s.minIdx >= 0 && s.minIdx < arr.length) hl[s.minIdx] = 'green';
const pointers = {};
if (s.left >= 0 && s.left < arr.length) pointers['L'] = s.left;
if (s.right >= 0 && s.right < arr.length) pointers['R'] = s.right;
if (s.mid >= 0 && s.mid < arr.length) pointers['mid'] = s.mid;
let viz = renderArray(arr, {highlights: hl, pointers});
if (s.mid >= 0 && s.mid < arr.length) {
viz += `<div style="margin-top:8px;font-size:13px;">比较:nums[${s.mid}]=${arr[s.mid]} vs nums[${s.right}]=${arr[s.right]} → ${arr[s.mid]>arr[s.right]?'mid > right → 右半':'mid ≤ right → 左半'}</div>`;
}
if (s.minIdx >= 0) {
viz += `<div class="current-answer">🏆 最小值 = ${arr[s.minIdx]},在索引 ${s.minIdx}</div>`;
}
$('vizArea').innerHTML = viz;
$('detailContent').innerHTML = `<div class="calc-block">${s.msg}</div>
<div style="margin-top:6px;font-size:13px;color:var(--text-secondary);">核心思路:与右端点比较,> 则旋转点在右,≤ 则旋转点在左(含mid)</div>`;
if (s.stage === 'done' && s.minIdx >= 0) {
$('resultContent').innerHTML = `<div class="final-answer">最小值为 <b>${arr[s.minIdx]}</b>(索引 ${s.minIdx})<br>时间复杂度 O(log n)</div>`;
}
$('hintText').textContent = s.msg;
const stages = ['init→初始化','calc→计算mid','goRight→右缩','goLeft→左缩','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 = 'nums=[3,4,5,1,2]';
buildSteps(examples[0].nums);
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 m = $('inputArea').value.match(/nums=\[([^\]]+)\]/);
if (!m) { alert('格式: nums=[3,4,5,1,2]'); return; }
const arr = m[1].split(',').map(Number);
buildSteps(arr);
stepCtrl.setSteps(steps.map((_,i)=>i)); render(0);
$('stepInfo').textContent = `步骤 1 / ${steps.length}`;
} catch(e) { alert('输入格式错误'); }
};
$('exampleSelect').onchange = () => {
const e = examples[parseInt($('exampleSelect').value)];
$('inputArea').value = `nums=[${e.nums}]`;
buildSteps(e.nums);
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 findMin(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
# 最小值在右半
left = mid + 1
else:
# 最小值在左半(含 mid)
right = mid
return nums[left]`, {lang:'Python'});
})();
</script>
</body>
</html>