一个可靠的策略是理解、规划,然后编码——永远不要直接跳到敲代码。结构化的方法能够早期发现边界情况,并清晰地传达你的思维过程。
分步框架
text
1. CLARIFY -> restate the problem, ask about inputs, ranges, edge cases
2. EXAMPLES -> work a small example by hand; note edge cases (empty, 1, dups)
3. BRUTE FORCE -> state the obvious solution and its complexity
4. OPTIMIZE -> spot the bottleneck; pick a pattern (hash, two-pointer, DP...)
5. CODE -> write clean, modular code while talking through it
6. TEST -> trace examples, check edges, fix bugs
7. ANALYZE -> state final time and space complexity
模式识别
| 信号 | 可能的技巧 | |--------|-----------------|| 排序数组 / 数对 | 双指针 / 二分查找 | | "subarray / substring" | 滑动窗口 / 前缀和 | | "count / seen before" | hash map | | "所有组合" | backtracking | | 重叠子问题 | 动态规划 | | graph / grid 可达性 | BFS / DFS |
思维方式示例
python
