Convert Figma logo to code with AI

azl397985856 logoleetcode

LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)

54,397
9,451
54,397
7

Top Related Projects

算法模板,最科学的刷题方式,最快速的刷题路径,你值得拥有~

Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

Everything you need to know to get the job.

刷算法全靠套路,认准 labuladong 就够了!English version supported! Crack LeetCode, not only how, but also why.

17,575

LeetCode Problems' Solutions

Quick Overview

The azl397985856/leetcode repository is a comprehensive collection of LeetCode problem solutions and explanations in multiple programming languages. It aims to help developers prepare for technical interviews and improve their problem-solving skills by providing detailed walkthroughs and optimized solutions for various LeetCode challenges.

Pros

  • Extensive coverage of LeetCode problems with solutions in multiple languages
  • Detailed explanations and thought processes for solving each problem
  • Regular updates and contributions from the community
  • Includes additional resources like interview preparation tips and algorithm explanations

Cons

  • Some solutions may not be optimized for all edge cases
  • The repository's large size can be overwhelming for beginners
  • Not all problems have solutions in every supported language
  • Some explanations may be more detailed in Chinese than in English

Code Examples

# Two Sum problem solution
def twoSum(nums, target):
    seen = {}
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            return [seen[complement], i]
        seen[num] = i
    return []
// Reverse Linked List solution
var reverseList = function(head) {
    let prev = null;
    let current = head;
    while (current !== null) {
        let nextTemp = current.next;
        current.next = prev;
        prev = current;
        current = nextTemp;
    }
    return prev;
};
// Valid Parentheses solution
public boolean isValid(String s) {
    Stack<Character> stack = new Stack<>();
    for (char c : s.toCharArray()) {
        if (c == '(' || c == '[' || c == '{') {
            stack.push(c);
        } else {
            if (stack.isEmpty()) return false;
            if (c == ')' && stack.peek() != '(') return false;
            if (c == ']' && stack.peek() != '[') return false;
            if (c == '}' && stack.peek() != '{') return false;
            stack.pop();
        }
    }
    return stack.isEmpty();
}

Getting Started

To use this repository:

  1. Clone the repository:
    git clone https://github.com/azl397985856/leetcode.git
    
  2. Navigate to the problem you want to study.
  3. Choose the solution in your preferred programming language.
  4. Read the explanation and study the code.
  5. Try to implement the solution yourself before checking the provided code.
  6. Practice regularly and contribute your own solutions if you have optimizations or alternative approaches.

Competitor Comparisons

算法模板,最科学的刷题方式,最快速的刷题路径,你值得拥有~

Pros of algorithm-pattern

  • More focused on algorithmic patterns and problem-solving strategies
  • Cleaner organization with categorized content
  • Includes visual aids and diagrams for better understanding

Cons of algorithm-pattern

  • Less comprehensive coverage of LeetCode problems
  • Fewer detailed explanations for individual problems
  • Limited language support (primarily Go)

Code Comparison

algorithm-pattern (Go):

func twoSum(nums []int, target int) []int {
    m := make(map[int]int)
    for i, num := range nums {
        if j, ok := m[target-num]; ok {
            return []int{j, i}
        }
        m[num] = i
    }
    return nil
}

leetcode (JavaScript):

var twoSum = function(nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (map.has(complement)) {
            return [map.get(complement), i];
        }
        map.set(nums[i], i);
    }
};

Both repositories offer valuable resources for algorithm learning and LeetCode problem-solving. algorithm-pattern focuses on patterns and strategies with a cleaner organization, while leetcode provides a more comprehensive collection of problems with detailed explanations. The choice between the two depends on individual learning preferences and goals.

Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)

Pros of LeetCodeAnimation

  • Provides animated visualizations of algorithms, making complex concepts easier to understand
  • Offers a unique learning experience through visual representations of problem-solving processes
  • Includes a variety of programming languages for solutions, catering to a wider audience

Cons of LeetCodeAnimation

  • Has fewer problems covered compared to leetcode
  • Lacks detailed explanations and analysis of time/space complexity for some problems
  • Updates less frequently, with fewer recent contributions

Code Comparison

LeetCodeAnimation (Python):

def twoSum(nums, target):
    hash_table = {}
    for i, num in enumerate(nums):
        if target - num in hash_table:
            return [hash_table[target - num], i]
        hash_table[num] = i
    return []

leetcode (JavaScript):

var twoSum = function(nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (map.has(complement)) return [map.get(complement), i];
        map.set(nums[i], i);
    }
};

Both repositories provide solutions to the "Two Sum" problem, but leetcode offers implementations in multiple languages and often includes more detailed explanations of the approach and complexity analysis.

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

Pros of LeetCode-Go

  • Written in Go, offering solutions for developers focused on this language
  • Comprehensive documentation with detailed explanations for each solution
  • Includes time and space complexity analysis for most problems

Cons of LeetCode-Go

  • Fewer total problems solved compared to leetcode
  • Less active community engagement and fewer contributors
  • Lacks multilingual support, focusing solely on Go

Code Comparison

LeetCode-Go (Go):

func twoSum(nums []int, target int) []int {
    m := make(map[int]int)
    for i, num := range nums {
        if j, ok := m[target-num]; ok {
            return []int{j, i}
        }
        m[num] = i
    }
    return nil
}

leetcode (JavaScript):

var twoSum = function(nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        if (map.has(target - nums[i])) {
            return [map.get(target - nums[i]), i];
        }
        map.set(nums[i], i);
    }
};

Both repositories provide solutions to LeetCode problems, but they cater to different audiences. LeetCode-Go is ideal for Go developers, offering in-depth explanations and complexity analysis. On the other hand, leetcode provides a broader range of solutions in multiple languages, making it more versatile for a diverse audience. The code comparison shows similar approaches to solving the "Two Sum" problem, with each implementation tailored to its respective language.

Everything you need to know to get the job.

Pros of interviews

  • More comprehensive coverage of interview topics beyond just LeetCode problems
  • Includes additional resources like books, articles, and interview preparation tips
  • Offers a wider range of programming languages in code examples

Cons of interviews

  • Less frequently updated compared to leetcode
  • Fewer detailed problem explanations and solution breakdowns
  • Smaller community engagement and fewer contributors

Code Comparison

interviews (Java):

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

leetcode (JavaScript):

var reverseList = function(head) {
    let prev = null;
    while (head) {
        [head.next, prev, head] = [prev, head, head.next];
    }
    return prev;
};

Both repositories provide solutions to the "Reverse Linked List" problem, but leetcode uses a more concise approach with JavaScript's destructuring assignment, while interviews uses a more traditional iterative approach in Java.

刷算法全靠套路,认准 labuladong 就够了!English version supported! Crack LeetCode, not only how, but also why.

Pros of fucking-algorithm

  • More comprehensive explanations and in-depth analysis of algorithms
  • Focuses on problem-solving techniques and thought processes
  • Includes detailed diagrams and visualizations to aid understanding

Cons of fucking-algorithm

  • Primarily in Chinese, which may limit accessibility for non-Chinese speakers
  • Less frequent updates compared to leetcode
  • Fewer problems covered overall

Code Comparison

leetcode:

var twoSum = function(nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (map.has(complement)) {
            return [map.get(complement), i];
        }
        map.set(nums[i], i);
    }
};

fucking-algorithm:

def twoSum(nums: List[int], target: int) -> List[int]:
    hashtable = dict()
    for i, num in enumerate(nums):
        if target - num in hashtable:
            return [hashtable[target - num], i]
        hashtable[nums[i]] = i
    return []

Both repositories provide solutions to LeetCode problems, but fucking-algorithm offers more detailed explanations and focuses on teaching problem-solving strategies. leetcode covers a wider range of problems and is updated more frequently. The code examples show similar approaches to the "Two Sum" problem, with leetcode using JavaScript and fucking-algorithm using Python.

17,575

LeetCode Problems' Solutions

Pros of leetcode (haoel)

  • More comprehensive coverage of LeetCode problems (500+ solutions)
  • Solutions provided in multiple programming languages (C++, Python, Java, etc.)
  • Includes detailed explanations and time/space complexity analysis for many solutions

Cons of leetcode (haoel)

  • Less frequent updates compared to the azl397985856 repository
  • Lacks a structured organization or categorization of problems
  • Some solutions may be outdated or not optimized for current LeetCode requirements

Code Comparison

leetcode (azl397985856):

var twoSum = function(nums, target) {
    const map = new Map();
    for(let i = 0; i < nums.length; i++) {
        if(map.has(target - nums[i])) {
            return [map.get(target - nums[i]), i];
        }
        map.set(nums[i], i);
    }
};

leetcode (haoel):

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> m;
        for (int i = 0; i < nums.size(); i++) {
            if (m.find(target - nums[i]) != m.end()) {
                return {m[target - nums[i]], i};
            }
            m[nums[i]] = i;
        }
        return {};
    }
};

Both implementations use a hash map to solve the Two Sum problem efficiently, but they are written in different languages (JavaScript vs. C++) and have slight variations in syntax and structure.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

LeetCode

Travis Travis Travis Travis Travis Travis

简体中文 | English


我们的 slogan 是: 只有熟练掌握基础的数据结构与算法,才能对复杂问题迎刃有余。

Star History Chart

🔥🔥🔥 我的《算法通关之路》出版了 🔥🔥🔥

我的新书《算法通关之路》出版了。

图片加载不出来如何解决?

https://github.com/fe-lucifer/fanqiang

力扣专属折扣

力扣免费题目已经有了很多经典的了,也覆盖了所有的题型,只是很多公司的真题都是锁定的。个人觉得如果你准备找工作的时候,可以买一个会员。另外会员很多leetbook 也可以看,结合学习计划,效率还是蛮高的。

现在力扣在每日一题基础上还搞了一个 plus 会员挑战,每天刷题可以获得积分,积分可以兑换力扣周边。

plus 会员挑战

如果你要买力扣会员的话,这里有我的专属力扣折扣:https://leetcode.cn/premium/?promoChannel=lucifer (年度会员多送两个月会员,季度会员多送两周会员)

:calendar:《91 天学算法》限时活动

很多教育机构宣传的 7 天,一个月搞定算法面试的,我大概都了解了下,不怎么靠谱。学习算法这东西,还是要靠积累,没有量变是不可能有质变的。还有的人选择看书,这是一个不错的选择。但是很多人选了过时的或者质量差的书,又或者不会去写书中给的练习题,导致效果很差。

基于这几个原因,我组织了一个 91 天刷题活动,通过一个相对比较长的时间(91 天)给出最新的学习路径,并强制大家打卡这种高强度练习来让大家**在 91 天后遇见更好的自己**。详细活动介绍可以点下方链接查看。另外往期的讲义也在下面了,大家可以看看合不合你的口味。

最后送给大家一句话: **坚持下去,会有突然间成长的一天**。

点此参与

1V1 辅导

如果大家觉得上面的集体活动效率比较低,我目前也接受 1v1 算法辅导,价格根据你的算法基础以及想要学习的内容而定感兴趣的可以加我微信,备注“算法辅导”,微信号 DevelopeEngineer。

:octocat: 仓库介绍

leetcode 题解,记录自己的 leetcode 解题之路。

本仓库目前分为五个部分:

  • 第一个部分是 leetcode 经典题目的解析,包括思路,关键点和具体的代码实现。

  • 第二部分是对于数据结构与算法的总结

  • 第三部分是 anki 卡片, 将 leetcode 题目按照一定的方式记录在 anki 中,方便大家记忆。

  • 第四部分是每日一题,每日一题是在交流群(包括微信和 qq)里进行的一种活动,大家一起 解一道题,这样讨论问题更加集中,会得到更多的反馈。而且 这些题目可以被记录下来,日后会进行筛选添加到仓库的题解模块。

  • 第五部分是计划, 这里会记录将来要加入到以上三个部分内容

:blue_book: 电子书

注意:这里的电子书并不是《算法通关之路》的电子版,而是本仓库内容的电子版!

在线阅读地址

限时免费下载!后期随时可能收费

可以去我的公众号《力扣加加》后台回复电子书获取!

epub 还是有动图的

另外有些内容只在公众号发布,因此大家觉得内容不错的话,可以关注一下。如果再给 ➕ 个星标就更棒啦!

:meat_on_bone: 仓库食用指南

  • 这里有一张互联网公司面试中经常考察的问题类型总结的思维导图,我们可以结合图片中的信息分析一下。

leetcode-zhihu

(图片来自 leetcode)

其中算法,主要是以下几种:

  • 基础技巧:分治、二分、贪心
  • 排序算法:快速排序、归并排序、计数排序
  • 搜索算法:回溯、递归、深度优先遍历,广度优先遍历,二叉搜索树等
  • 图论:最短路径、最小生成树
  • 动态规划:背包问题、最长子序列

数据结构,主要有如下几种:

  • 数组与链表:单 / 双向链表
  • 栈与队列
  • 哈希表
  • 堆:最大堆 / 最小堆
  • 树与图:最近公共祖先、并查集
  • 字符串:前缀树(字典树) / 后缀树

我在网上找到一份 《Interview Cheat Sheet》,这个 PDF 列举了面试的**模板步骤**。,详细指示了如何一步步完成面试。

这个 pdf 开头就提到了好的代码三个标准:

  1. 可读性
  2. 时间复杂度
  3. 空间复杂度

这写的太好了。

紧接着,列举了 15 算法面试的步骤。比如步骤一:当面试官提问完后,你需要先下来关键点(之后再下面写注释和代码) 看完我的感受就是,面试只要按照这个来做,成功率蹭蹭提升

数据结构与算法的总结

:exclamation: 怎么刷 LeetCode?

:computer: 插件

或许是一个可以改变你刷题效率的浏览器扩展插件。

插件地址:https://chrome.google.com/webstore/detail/leetcode-cheatsheet/fniccleejlofifaakbgppmbbcdfjonle?hl=en-US。

不能访问谷歌商店的朋友可以去我的公众号回复插件获取离线版。强烈推荐大家使用谷歌商店安装, 这样如果有更新可以自动安装,毕竟咱们的插件更新还是蛮快的。

另外大家也可以使用 zerotrac 开发的用于计算力扣中题目分数的网站。这里的分数指的是竞赛分,大家可以根据自己的竞赛分选择稍微比自己竞赛分高一点的题目进行练习,注意这个只是根据通过人数等计算的一个预估分数。地址:https://zerotrac.github.io/leetcode_problem_rating/

精选题解

leetcode 经典题目的解析(200 多道)

这里仅列举具有**代表性题目**,并不是全部题目

目前更新了 200 多道题解,加上专题涉及的题目,差不多有 300 道。

简单难度题目合集

这里的题目难度比较小, 大多是模拟题,或者是很容易看出解法的题目,另外简单题目一般使用暴力法都是可以解决的。 这个时候只有看一下数据范围,思考下你的算法复杂度就行了。

当然也不排除很多 hard 题目也可以暴力模拟,大家平时多注意数据范围即可。

以下是我列举的经典题目(带 91 字样的表示出自 91 天学算法活动):

中等难度题目合集

中等题目是力扣比例最大的部分,因此这部分我的题解也是最多的。 大家不要太过追求难题,先把中等难度题目做熟了再说。

这部分的题目要不需要我们挖掘题目的内含信息, 将其抽象成简单题目。 要么是一些写起来比较麻烦的题目, 一些人编码能力不行就挂了。因此大家一定要自己做, 即使看了题解 ”会了“,也要自己码一遍。自己不亲自写一遍,里面的细节永远不知道。

以下是我列举的经典题目(带 91 字样的表示出自 91 天学算法活动):

困难难度题目合集

困难难度题目从类型上说多是:

  • 图
  • 设计题
  • 游戏场景题目
  • 中等题目的 follow up

从解法上来说,多是:

  • 图算法
  • 动态规划
  • 二分法
  • DFS & BFS
  • 状态压缩
  • 剪枝

从逻辑上说, 要么就是非常难想到,要么就是非常难写代码。 由于有时候需要组合多种算法,因此这部分题目的难度是最大的。

这里我总结了几个技巧:

  1. 看题目的数据范围, 看能否暴力模拟
  2. 暴力枚举所有可能的算法往上套,比如图的题目。
  3. 对于代码非常难写的题目,可以总结和记忆解题模板,减少解题压力
  4. 对于组合多种算法的题目,先尝试简化问题,将问题划分成几个小问题,然后再组合起来。

以下是我列举的经典题目(带 91 字样的表示出自 91 天学算法活动):

:trident:  anki 卡片

Anki 主要分为两个部分:一部分是关键点到题目的映射,另一部分是题目到思路,关键点,代码的映射。

全部卡片都在 anki-card

使用方法:

anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后选中你下载好的文件,确定即可。

更多关于 anki 使用方法的请查看 anki 官网

关于我

大家也可以加我微信好友进行交流!

:chart_with_upwards_trend: 大事件

  • 2019-07-10 :纪念项目 Star 突破 1W 的一个短文, 记录了项目的"兴起"之路,大家有兴趣可以看一下,如果对这个项目感兴趣,请点击一下 Star, 项目会**持续更新**,感谢大家的支持。

  • 2019-10-08: 纪念 LeetCode 项目 Star 突破 2W,并且 Github 搜索“LeetCode”,排名第一。

  • 2020-04-12: 项目突破三万 Star。

  • 2020-04-14: 官网力扣加加上线啦 💐💐💐💐💐,有专题讲解,每日一题,下载区和视频题解,后续会增加更多内容,还不赶紧收藏起来?地址:http://leetcode-solution.cn/

  • 2021-02-23: star 破四万

:gift_heart: 贡献

  • 如果有想法和创意,请提 issue 或者进群提
  • 如果想贡献增加题解或者翻译, 可以参考 贡献指南

    关于如何提交题解,我写了一份 指南

  • 如果需要修改项目中图片,这里 存放了项目中绘制图的源代码,大家可以用 draw.io 打开进行编辑。

:love_letter: 鸣谢

感谢为这个项目作出贡献的所有 小伙伴

License

CC BY-NC-ND 4.0