LeetCode 是全球知名的编程练习平台,尤其对于准备技术面试的开发者来说至关重要。
本文从 Leetcode 中精选大概 300 左右的题目,去除了某些繁杂但是没有多少算法思想的题目,同时保留了面试中经常被问到的经典题目。将详细介绍如何使用 Java 解决 LeetCode 问题,涵盖基础技巧、常见模式以及高级优化策略。
推荐练习题目
| 类别 | 简单题 | 中等题 | 困难题 |
|---|---|---|---|
| 数组 | 两数之和 | 三数之和 | 接雨水 |
| 链表 | 反转链表 | 两数相加 | 合并K个有序链表 |
| 树 | 最大深度 | 二叉搜索树中序后继 | 二叉树序列化 |
| DP | 爬楼梯 | 零钱兑换 | 编辑距离 |
| 回溯 | 子集 | 组合总和 | N皇后 |
通过系统性的 LeetCode 练习,结合 Java 语言特性,你能够显著提升算法能力和面试表现。建议从简单题开始,逐步提高难度,每个题目尝试多种解法并比较优劣。坚持每天练习,三个月后会有显著进步。
一、LeetCode 入门指南
1.1 为什么选择 Java 刷 LeetCode
Java 是 LeetCode 上最受欢迎的语言之一,原因包括:
- 严格的类型系统有助于面试时减少错误
- 丰富的内置数据结构和算法库
- 企业级应用广泛,面试中常被要求使用
1.2 LeetCode 环境设置
在 LeetCode 中使用 Java 需要注意:
- 默认模板使用类名为
Solution - 方法需定义为
public - 无需导入常见包(如
java.util.*)
// 典型 LeetCode Java 模板
class Solution {
public int[] twoSum(int[] nums, int target) {
// 实现代码
}
}
二、Java 基础数据结构应用
2.1 数组与字符串处理
例题 1:两数之和 (Two Sum)
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
关键点:
- 使用 HashMap 存储值和索引
- 时间复杂度 O(n),空间复杂度 O(n)
2.2 链表操作
例题 2:反转链表 (Reverse Linked List)
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}
技巧:
- 使用三个指针:prev, curr, next
- 迭代法空间复杂度 O(1)
三、算法思想与 Java 实现
3.1 二分查找
例题 3:搜索旋转排序数组 (Search in Rotated Sorted Array)
class Solution {
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[left] <= nums[mid]) {
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}
要点:
- 确定有序区间
- 处理边界条件
3.2 动态规划
例题 4:最长递增子序列 (Longest Increasing Subsequence)
class Solution {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) return 0;
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
int maxans = 1;
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
maxans = Math.max(maxans, dp[i]);
}
return maxans;
}
}
优化: 可改进为 O(n log n) 解法
四、Java 高级特性应用
4.1 优先队列 (PriorityQueue)
例题 5:前 K 个高频元素 (Top K Frequent Elements)
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : nums) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
PriorityQueue<Integer> heap = new PriorityQueue<>(
(a, b) -> frequencyMap.get(a) - frequencyMap.get(b)
);
for (int num : frequencyMap.keySet()) {
heap.add(num);
if (heap.size() > k) {
heap.poll();
}
}
int[] result = new int[k];
for (int i = k - 1; i >= 0; i--) {
result[i] = heap.poll();
}
return result;
}
}
4.2 回溯算法
例题 6:全排列 (Permutations)
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}
private void backtrack(List<List<Integer>> result, List<Integer> temp, int[] nums) {
if (temp.size() == nums.length) {
result.add(new ArrayList<>(temp));
} else {
for (int i = 0; i < nums.length; i++) {
if (temp.contains(nums[i])) continue;
temp.add(nums[i]);
backtrack(result, temp, nums);
temp.remove(temp.size() - 1);
}
}
}
}
五、性能优化技巧
- 避免不必要的对象创建:在循环中尽量减少 new 对象
- 使用 StringBuilder 处理字符串拼接
- 预先分配集合大小:如
new ArrayList<>(capacity) - 位运算优化:某些问题可用位运算提高速度
- 记忆化技术:缓存中间结果减少重复计算
六、面试准备建议
- 分类练习:按题目类型(DP、回溯、贪心等)系统练习
- 手写代码:练习在白板或纸上编写无语法错误的代码
- 复杂度分析:对每个解法能准确分析时间和空间复杂度
- 测试用例:考虑边界情况和特殊输入
- 交流思路:练习解释解题思路的过程