LeetCode精选

# LeetCode精选

# 🎯 LeetCode简介

LeetCode是全球最大的在线编程练习平台,提供了丰富的算法和数据结构题目,是程序员面试准备的首选平台。

# 📊 题目分类

# 按难度分类

  • 🟢 Easy (简单):基础语法和简单逻辑
  • 🟡 Medium (中等):需要算法思维和数据结构知识
  • 🔴 Hard (困难):复杂算法和高级数据结构

# 按类型分类

  • 数组与字符串
  • 链表
  • 栈与队列
  • 树与图
  • 动态规划
  • 回溯算法
  • 贪心算法
  • 数学与位运算

# 🏆 热门题目精选

# 1. 两数之和 (Two Sum) 🟢

题目描述: 给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。

解法一:暴力法

public int[] twoSum(int[] nums, int target) {
    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                return new int[]{i, j};
            }
        }
    }
    return new int[]{};
}
// 时间复杂度:O(n²),空间复杂度:O(1)

解法二:哈希表

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);
    }
    return new int[]{};
}
// 时间复杂度:O(n),空间复杂度:O(n)

# 2. 反转链表 (Reverse Linked List) 🟢

题目描述: 给你单链表的头节点head,请你反转链表,并返回反转后的链表。

解法一:迭代

public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode current = head;
    
    while (current != null) {
        ListNode nextTemp = current.next;
        current.next = prev;
        prev = current;
        current = nextTemp;
    }
    
    return prev;
}

解法二:递归

public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    
    ListNode reversedHead = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    
    return reversedHead;
}

# 3. 最大子数组和 (Maximum Subarray) 🟡

题目描述: 给你一个整数数组nums,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

解法:动态规划(Kadane算法)

public int maxSubArray(int[] nums) {
    int maxSum = nums[0];
    int currentSum = nums[0];
    
    for (int i = 1; i < nums.length; i++) {
        // 要么继续之前的子数组,要么重新开始
        currentSum = Math.max(nums[i], currentSum + nums[i]);
        maxSum = Math.max(maxSum, currentSum);
    }
    
    return maxSum;
}
// 时间复杂度:O(n),空间复杂度:O(1)

# 4. 爬楼梯 (Climbing Stairs) 🟢

题目描述: 假设你正在爬楼梯。需要n阶你才能到达楼顶。每次你可以爬1或2个台阶。你有多少种不同的方法可以爬到楼顶呢?

解法:动态规划

public int climbStairs(int n) {
    if (n <= 2) return n;
    
    int first = 1, second = 2;
    for (int i = 3; i <= n; i++) {
        int third = first + second;
        first = second;
        second = third;
    }
    
    return second;
}
// 时间复杂度:O(n),空间复杂度:O(1)

# 5. 有效的括号 (Valid Parentheses) 🟢

题目描述: 给定一个只包括'(',')','{','}','[',']'的字符串s,判断字符串是否有效。

解法:栈

public boolean isValid(String s) {
    Stack<Character> stack = new Stack<>();
    Map<Character, Character> mapping = new HashMap<>();
    mapping.put(')', '(');
    mapping.put('}', '{');
    mapping.put(']', '[');
    
    for (char c : s.toCharArray()) {
        if (mapping.containsKey(c)) {
            // 右括号
            if (stack.isEmpty() || stack.pop() != mapping.get(c)) {
                return false;
            }
        } else {
            // 左括号
            stack.push(c);
        }
    }
    
    return stack.isEmpty();
}

# 📈 刷题策略

# 初级阶段 (0-100题)

  1. 熟悉平台:了解LeetCode界面和提交流程
  2. 基础题型:数组、字符串、链表基础操作
  3. 简单算法:双指针、哈希表、简单递归
  4. 目标:每天1-2题,重点理解思路

# 中级阶段 (100-300题)

  1. 算法进阶:动态规划、回溯、贪心
  2. 数据结构:树、图、堆、并查集
  3. 优化思维:时间复杂度和空间复杂度优化
  4. 目标:每天2-3题,注重解法多样性

# 高级阶段 (300+题)

  1. 困难题目:挑战Hard级别题目
  2. 系统总结:按类型总结解题模板
  3. 竞赛参与:参加周赛和双周赛
  4. 目标:保持手感,准备面试

# 💡 解题技巧

  1. 读题仔细:理解题意和约束条件
  2. 举例分析:用具体例子理解问题
  3. 暴力开始:先想出暴力解法
  4. 逐步优化:分析瓶颈,优化算法
  5. 代码实现:注意边界条件和异常处理
  6. 测试验证:用例子验证代码正确性

# 📚 推荐资源

  • LeetCode官网:https://leetcode.com/
  • LeetCode中国:https://leetcode-cn.com/
  • 题解网站:代码随想录、labuladong
  • 视频教程:花花酱、Back To Back SWE

开始您的LeetCode刷题之旅!🚀