LeetCode 教师
一个交互式技术面试准备工具,能够生成带有真实产品挑战、模式识别训练和多语言支持的编码实验环境。
此技能的功能
将技术面试准备转变为互动、实用的体验:
- 交互式编码实验环境 - 基于浏览器的编程环境,即时反馈
- 多语言支持 - Python、TypeScript、Kotlin、Swift
- 真实产品挑战 - 来自真实公司的实际场景
- 模式识别 - 学习 20 种核心编码模式
- 渐进难度 - 简单 → 中等 → 困难 → 专家级
- 即时反馈 - 实时运行测试并附带详细解释
- 技巧教学 - 掌握解题方法
为什么此技能很重要
传统 LeetCode 练习:
- 抽象、脱离实际的问题
- 缺乏模式识别指导
- 试错法
- 对初学者有压迫感
- 语言选项有限
使用此技能:
- 真实产品场景
- 基于模式的学习
- 有指导的解题过程
- 渐进难度曲线
- 多语言练习
- 互动、有趣的界面
核心原则
1. 模式优先学习
- 识别问题模式
- 应用经过验证的模板
- 通过练习建立直觉
- 一次掌握一种模式
2. 真实产品背景
- Instagram 信息流排名
- Uber 行程匹配
- Netflix 推荐
- Slack 消息搜索
- Amazon 库存管理
3. 渐进难度
- 从基础开始
- 逐渐增加复杂度
- 解锁高级模式
- 追踪技能进步
4. 多语言精通
- 用目标语言练习
- 比较不同实现
- 学习语言特定的技巧
- 用任何语言面试
5. 交互式学习
- 在浏览器中编写代码
- 即时运行测试
- 卡住时获取提示
- 查看最优解
- 追踪进度
覆盖的问题模式
数组与字符串模式
1. 双指针
模式:用两个指针扫描数组
使用场景:需要查找数对、三元组或子数组时
示例:“找出 Instagram 中互相关注的用户”
复杂度:O(n) 时间,O(1) 空间
2. 滑动窗口
模式:维护一个滑过数组的窗口
使用场景:需要查找具有特定属性的子数组时
示例:“找出最近 N 条推文中的热门话题”
复杂度:O(n) 时间,O(k) 空间
3. 快慢指针
模式:两个指针以不同速度移动
使用场景:检测循环、查找中间元素
示例:“检测包管理器中的循环依赖”
复杂度:O(n) 时间,O(1) 空间
树与图模式
4. 树 BFS
模式:使用队列进行层级遍历
使用场景:需要逐层处理时
示例:“按连接度显示朋友”
复杂度:O(n) 时间,O(w) 空间(w = 最大宽度)
5. 树 DFS
模式:前序、中序或后序遍历
使用场景:需要探索所有路径时
示例:“查找文件系统中的所有路径”
复杂度:O(n) 时间,O(h) 空间(h = 高度)
6. 图 BFS
模式:逐层探索邻居节点
使用场景:最短路径、基于层级的探索
示例:“在 LinkedIn 上找到最短连接路径”
复杂度:O(V + E) 时间,O(V) 空间
7. 图 DFS
模式:回溯前尽可能深入探索
使用场景:寻路、环检测
示例:“检测社交图中的循环引用”
复杂度:O(V + E) 时间,O(V) 空间
8. 拓扑排序
模式:按依赖关系排序节点
使用场景:任务调度、构建系统
示例:“根据先修课程排列课程”
复杂度:O(V + E) 时间,O(V) 空间
动态规划模式
9. 0/1 背包
模式:每个物品选或不选
使用场景:带约束的优化问题
示例:“在预算内选择最佳广告”
复杂度:O(n * capacity) 时间和空间
10. 无限背包
模式:可以无限次使用物品
使用场景:硬币找零、组合问题
示例:“最少的交易次数以达到余额”
复杂度:O(n * target) 时间和空间
11. 斐波那契数
模式:当前状态依赖于之前的状态
使用场景:爬楼梯、铺砖问题
示例:“导航 app 屏幕的方式数”
复杂度:O(n) 时间,O(1) 空间(优化后)
12. 最长公共子序列
模式:比较两个序列
使用场景:差异工具、编辑距离
示例:“查找相似的代码片段”
复杂度:O(m * n) 时间和空间
其他核心模式
13. 修改版二分搜索
模式:在排序或旋转数组上进行二分搜索
使用场景:在 O(log n) 时间内进行搜索
示例:“找到引入 bug 的版本”
复杂度:O(log n) 时间,O(1) 空间
14. 前 K 个元素
模式:使用堆追踪最大的 K 个或最小的 K 个元素
使用场景:查找前几个项目
示例:“获取前 K 个热门话题标签”
复杂度:O(n log k) 时间,O(k) 空间
15. K 路归并
模式:合并 K 个有序数组/列表
使用场景:合并排序数据
示例:“合并 K 个用户的活动流”
复杂度:O(n log k) 时间,O(k) 空间
16. 回溯
模式:尝试所有可能性并进行剪枝
使用场景:生成排列、组合
示例:“生成所有有效的括号组合”
复杂度:各不相同,通常为指数级
17. 并查集
模式:追踪连通分量
使用场景:网络连通性、分组
示例:“查找相连的朋友群”
复杂度:每次操作均摊 O(α(n))
18. 区间
模式:合并、插入或查找重叠区间
使用场景:日历排程、时间范围
示例:“查找空闲的会议时间段”
复杂度:O(n log n) 时间,O(n) 空间
19. 单调栈
模式:维护递增/递减栈
使用场景:下一个更大/更小元素
示例:“股票价格跨度计算”
复杂度:O(n) 时间,O(n) 空间
20. 字典树
模式:用于字符串操作的前缀树
使用场景:自动补全、前缀匹配
示例:“实现搜索自动补全”
复杂度:每次操作 O(m)(m = 单词长度)
真实产品挑战示例
简单级别
Instagram:点赞计数器
真实场景:统计用户帖子今天被点赞的次数
模式:哈希表
数据结构:字典/HashMap
语言:Python、TypeScript、Kotlin、Swift
Slack:未读消息
真实场景:查找频道中的第一条未读消息
模式:带标志的线性搜索
数据结构:数组
教授技能:提前终止
Uber:计算车费
真实场景:根据距离和时间计算行程费用
模式:简单计算
数据结构:数字
教授技能:数学运算、四舍五入
中等难度
Netflix:前 N 个推荐
真实场景:按评分查找前 N 部电影
模式:前 K 个元素(堆)
数据结构:优先队列
教授技能:堆操作、部分排序
Amazon:库存管理
真实场景:查找库存不足的产品
模式:带阈值的过滤
数据结构:数组 + HashMap
教授技能:多条件筛选
Twitter:热门话题标签
真实场景:查找时间窗口内最常用的标签
模式:滑动窗口 + 频率统计
数据结构:队列 + HashMap
教授技能:基于时间的窗口管理
LinkedIn:连接度
真实场景:查找两个用户之间的连接路径
模式:BFS
数据结构:图(邻接表)
教授技能:最短路径、层级追踪
困难级别
Google Calendar:查找会议时间段
真实场景:查找所有参会者的空闲时间段
模式:区间合并
数据结构:区间数组
教授技能:排序、合并重叠区间
Spotify:播放列表随机播放
真实场景:真正的随机洗牌,避免艺术家重复
模式:修改版 Fisher-Yates
数据结构:数组
教授技能:带约束的随机化
GitHub:合并冲突解决
真实场景:查找文件中的最长公共子序列
模式:动态规划(LCS)
数据结构:二维数组
教授技能:DP 状态定义、优化
Airbnb:搜索排名
真实场景:根据多个加权标准对房源进行排名
模式:自定义排序 + 堆
数据结构:带比较器的优先队列
教授技能:复杂比较、打破平局
交互式实验环境示例
Python 实验环境
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🚀 LeetCode 教师 - 两数之和(Instagram 点赞)</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'SF Mono', 'Monaco', 'Courier New', monospace;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
color: white;
}
.container {
max-width: 1400px;
margin: 0 auto;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.panel {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
h1 {
font-size: 2.5em;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.difficulty {
display: inline-block;
padding: 5px 15px;
border-radius: 20px;
font-size: 0.9em;
font-weight: bold;
margin-bottom: 20px;
}
.easy { background: #4CAF50; }
.medium { background: #FF9800; }
.hard { background: #F44336; }
.problem {
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
line-height: 1.6;
}
.code-editor {
width: 100%;
min-height: 400px;
background: #1e1e1e;
color: #d4d4d4;
font-family: 'SF Mono', monospace;
font-size: 14px;
padding: 20px;
border-radius: 10px;
border: none;
resize: vertical;
}
.controls {
display: flex;
gap: 10px;
margin: 20px 0;
}
.btn {
padding: 12px 30px;
border: none;
border-radius: 10px;
font-size: 1em;
font-weight: bold;
cursor: pointer;
transition: transform 0.2s;
}
.btn-run {
background: linear-gradient(135deg, #4CAF50, #45a049);
color: white;
}
.btn-hint {
background: linear-gradient(135deg, #FF9800, #F57C00);
color: white;
}
.btn-solution {
background: linear-gradient(135deg, #2196F3, #1976D2);
color: white;
}
.btn:hover { transform: translateY(-2px); }
.output {
background: #1e1e1e;
color: #4CAF50;
padding: 20px;
border-radius: 10px;
min-height: 100px;
font-family: monospace;
white-space: pre-wrap;
margin-top: 20px;
}
.test-case {
background: rgba(255, 255, 255, 0.05);
padding: 15px;
border-radius: 8px;
margin: 10px 0;
border-left: 4px solid #4CAF50;
}
.test-failed {
border-left-color: #F44336;
}
.stats {
display: flex;
justify-content: space-around;
margin: 20px 0;
padding: 20px;
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
}
.stat {
text-align: center;
}
.stat-value {
font-size: 2em;
font-weight: bold;
color: #FFD700;
}
.pattern-badge {
display: inline-block;
background: rgba(255, 215, 0, 0.2);
color: #FFD700;
padding: 5px 15px;
border-radius: 15px;
margin: 5px;
font-size: 0.9em;
}
</style>
</head>
<body>
<div class="container">
<!-- 左侧面板:问题 -->
<div class="panel">
<h1>🎯 两数之和</h1>
<span class="difficulty easy">简单</span>
<span class="pattern-badge">模式:哈希表</span>
<span class="pattern-badge">数组</span>
<div class="problem">
<h3>📱 真实产品场景:Instagram 点赞</h3>
<p>你正在构建 Instagram 的“相互点赞”功能。给定一个点赞你帖子的用户 ID 数组和一个目标和,找出两个用户 ID 之和等于该目标值。</p>
<h4 style="margin-top: 20px;">问题:</h4>
<p>给定一个整数数组 <code>nums</code> 和一个整数 <code>target</code>,返回两个数之和等于 <code>target</code> 的索引。</p>
<h4 style="margin-top: 20px;">示例:</h4>
<code style="display: block; padding: 10px; background: rgba(0,0,0,0.3); border-radius: 5px;">
输入: nums = [2, 7, 11, 15], target = 9<br>
输出: [0, 1]<br>
解释: nums[0] + nums[1] = 2 + 7 = 9
</code>
<h4 style="margin-top: 20px;">约束条件:</h4>
<ul style="margin-left: 20px;">
<li>2 ≤ nums.length ≤ 10⁴</li>
<li>只有唯一有效的答案</li>
<li>不能使用同一个元素两次</li>
</ul>
</div>
<div class="stats">
<div class="stat">
<div class="stat-value" id="testsRun">0</div>
<div>已运行测试</div>
</div>
<div class="stat">
<div class="stat-value" id="testsPassed">0</div>
<div>已通过</div>
</div>
<div class="stat">
<div class="stat-value" id="attempts">0</div>
<div>尝试次数</div>
</div>
</div>
<div id="hints" style="margin-top: 20px;"></div>
</div>
<!-- 右侧面板:代码编辑器 -->
<div class="panel">
<h2>💻 你的解决方案(Python)</h2>
<textarea class="code-editor" id="codeEditor">def two_sum(nums, target):
"""
找到两个数之和等于目标值。
参数:
nums: 整数列表
target: 目标和
返回:
两个索引的列表
时间复杂度: O(n²) - 暴力法
空间复杂度: O(1)
"""
pass
</textarea>
<div class="controls">
<button class="btn btn-run" onclick="runCode()">▶ 运行</button>
<button class="btn btn-hint" onclick="showHint()">💡 提示</button>
<button class="btn btn-solution" onclick="showSolution()">🔍 解决方案</button>
</div>
<div class="output" id="output">
点击“运行”按钮测试你的解决方案...
</div>
</div>
</div>
<script>
// 定义测试用例
const testCases = [
{ nums: [2, 7, 11, 15], target: 9, expected: [0, 1] },
{ nums: [3, 2, 4], target: 6, expected: [1, 2] },
{ nums: [3, 3], target: 6, expected: [0, 1] }
];
let state = { testsRun: 0, testsPassed: 0, attempts: 0 };
function runCode() {
const code = document.getElementById('codeEditor').value;
try {
const func = new Function('nums', 'target', code);
const result = func(testCases[0].nums, testCases[0].target);
// TODO: 添加更完整的测试
state.testsRun++;
state.attempts++;
output(result);
} catch (e) {
output('错误: ' + e.message);
}
}
function output(text) {
document.getElementById('output').textContent = text;
document.getElementById('testsRun').textContent = state.testsRun;
document.getElementById('testsPassed').textContent = state.testsPassed;
document.getElementById('attempts').textContent = state.attempts;
}
function showHint() {
const hints = [
'💡 提示 1: 尝试使用哈希表存储已访问的元素',
'💡 提示 2: 对于每个元素,检查 target - nums[i] 是否在哈希表中',
'💡 提示 3: 如果找到,返回当前索引和存储的索引'
];
document.getElementById('hints').innerHTML = hints.join('<br>');
}
function showSolution() {
const solution = `def two_sum(nums, target):
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []`;
document.getElementById('output').textContent = '🔍 最优解:\n\n' + solution;
}
</script>
</body>
</html>
入门指南
- 将此技能添加到你的 AI 代理中
- 要求“生成一个 {主题} 的 LeetCode 风格练习”
- 指定你的目标编程语言
- 选择难度级别
- 开始在浏览器中编码!
使用命令:
/leetcode generate topic="两数之和" language="Python" difficulty="easy"/leetcode pattern "滑动窗口"/leetcode challenge "Instagram"