Top Related Projects
《Hello 算法》:动画图解、一键运行的数据结构与算法教程。支持简中、繁中、English、日本語,提供 Python, Java, C++, C, C#, JS, Go, Swift, Rust, Ruby, Kotlin, TS, Dart 等代码实现
《代码随想录》LeetCode 刷题攻略:200道经典题目刷题顺序,共60w字的详细图解,视频难点剖析,50余张思维导图,支持C++,Java,Python,Go,JavaScript等多语言版本,从此算法学习不再迷茫!🔥🔥 来看看,你会发现相见恨晚!🚀
LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)
✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计
Quick Overview
The "fucking-algorithm" repository on GitHub, created by labuladong, is a comprehensive collection of algorithms and data structures, accompanied by detailed explanations and solutions to common coding problems. The repository serves as a valuable resource for developers, students, and anyone interested in improving their problem-solving skills and expanding their algorithmic knowledge.
Pros
- Comprehensive Coverage: The repository covers a wide range of algorithmic topics, including dynamic programming, greedy algorithms, graph theory, and more, making it a one-stop-shop for algorithm learning.
- Detailed Explanations: The author, labuladong, provides in-depth explanations and step-by-step solutions for each problem, making it easier for users to understand the underlying concepts and approaches.
- Practical Applications: The problems and solutions presented in the repository are often drawn from real-world scenarios, making the content more relevant and applicable to everyday programming tasks.
- Active Community: The repository has a large and active community of contributors and users, who provide feedback, suggestions, and additional solutions, further enhancing the project's value.
Cons
- Language Specific: The repository is primarily focused on algorithms and solutions in the Python and Java programming languages, which may limit its usefulness for developers working in other languages.
- Lack of Structured Curriculum: While the repository covers a wide range of topics, it may lack a structured curriculum or learning path, which could make it challenging for beginners to navigate and progress through the content.
- Potential Outdated Content: As with any open-source project, there is a possibility that some of the content in the repository may become outdated over time, requiring regular updates and maintenance.
- Overwhelming for Beginners: The sheer volume of content and the complexity of some of the algorithms presented in the repository may be overwhelming for beginners, who may benefit from a more gradual and structured approach to learning.
Code Examples
Here are a few short code examples from the "fucking-algorithm" repository:
- Reversing a Linked List:
def reverseList(head: ListNode) -> ListNode:
prev, curr = None, head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
return prev
This code demonstrates the implementation of the "Reverse Linked List" problem, where the goal is to reverse the order of the nodes in a singly-linked list.
- Climbing Stairs:
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
This code demonstrates the implementation of the "Climbing Stairs" problem, where the goal is to find the number of unique ways to climb to the top of a staircase with n steps, given that you can either take 1 step or 2 steps at a time.
- Merge Two Sorted Lists:
def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode()
curr = dummy
while l1 and l2:
if l1.val < l2.val:
curr.next = l1
l1 = l1.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
if l1:
curr.next = l1
if l2:
curr.next = l2
return dummy.next
This code demonstrates the implementation of the "Merge Two Sorted Lists" problem, where the goal is to merge two sorted linked lists into a single sorted linked list.
Getting Started
To get started with the "fucking-algorithm" repository, follow these steps:
- Clone the repository to your local machine:
git clone https://github.com/labuladong/fucking-algorithm.git
- Navigate
Competitor Comparisons
《Hello 算法》:动画图解、一键运行的数据结构与算法教程。支持简中、繁中、English、日本語,提供 Python, Java, C++, C, C#, JS, Go, Swift, Rust, Ruby, Kotlin, TS, Dart 等代码实现
Pros of hello-algo
- Offers implementations in multiple programming languages (C, C++, Java, Python, etc.)
- Provides interactive visualizations for algorithms and data structures
- Includes a comprehensive e-book with detailed explanations
Cons of hello-algo
- Less extensive coverage of advanced algorithms compared to fucking-algorithm
- Newer project with potentially fewer community contributions and discussions
- May lack some of the in-depth problem-solving strategies found in fucking-algorithm
Code Comparison
hello-algo (Python):
def binary_search(nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
else:
return mid
return -1
fucking-algorithm (Java):
int binarySearch(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;
else if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid - 1;
}
return -1;
}
Both repositories provide excellent resources for learning algorithms and data structures. hello-algo offers a more visual and interactive approach with multi-language support, while fucking-algorithm provides a deeper dive into problem-solving techniques and advanced algorithms.
《代码随想录》LeetCode 刷题攻略:200道经典题目刷题顺序,共60w字的详细图解,视频难点剖析,50余张 思维导图,支持C++,Java,Python,Go,JavaScript等多语言版本,从此算法学习不再迷茫!🔥🔥 来看看,你会发现相见恨晚!🚀
Pros of leetcode-master
- More comprehensive coverage of LeetCode problems, with over 300 solutions
- Includes detailed explanations and diagrams for complex algorithms
- Regularly updated with new problems and solutions
Cons of leetcode-master
- Primarily focused on C++ solutions, which may not be ideal for all users
- Less emphasis on general problem-solving strategies compared to fucking-algorithm
Code Comparison
leetcode-master (C++):
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
auto it = map.find(target - nums[i]);
if (it != map.end()) {
return {it->second, i};
}
map[nums[i]] = i;
}
return {};
}
};
fucking-algorithm (Java):
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");
}
}
Both repositories offer valuable resources for learning algorithms and solving LeetCode problems. leetcode-master provides a more extensive collection of problems and solutions, while fucking-algorithm focuses on teaching problem-solving strategies and techniques. The choice between the two depends on the user's preferred programming language and learning style.
LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)
Pros of leetcode
- Covers a wider range of LeetCode problems, providing solutions in multiple programming languages
- Includes detailed explanations and time/space complexity analyses for each solution
- Offers a more comprehensive approach to LeetCode preparation, including company-specific problem lists
Cons of leetcode
- Less focus on algorithmic patterns and problem-solving techniques compared to fucking-algorithm
- May be overwhelming for beginners due to the sheer volume of problems and solutions
- Lacks the structured learning path and in-depth explanations found in fucking-algorithm
Code Comparison
fucking-algorithm (Java):
public ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
leetcode (JavaScript):
var reverseList = function(head) {
let prev = null, curr = head;
while (curr) {
[curr.next, prev, curr] = [prev, curr, curr.next];
}
return prev;
};
Both repositories provide solutions to common LeetCode problems, but they differ in their approach and focus. fucking-algorithm emphasizes understanding algorithmic patterns and problem-solving techniques, while leetcode offers a broader range of problems and solutions in multiple languages. The code comparison shows similar implementations of reversing a linked list, with fucking-algorithm using a more traditional approach in Java and leetcode showcasing a concise ES6 solution in JavaScript.
✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
Pros of LeetCode-Go
- Focuses specifically on Go language implementations
- Includes detailed explanations and comments in the code
- Covers a wide range of LeetCode problems with consistent structure
Cons of LeetCode-Go
- Limited to Go language, less accessible for users of other languages
- May not provide as in-depth algorithmic explanations as fucking-algorithm
Code Comparison
LeetCode-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
}
fucking-algorithm:
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");
}
Both repositories provide high-quality algorithm implementations and explanations, but they cater to different audiences. LeetCode-Go is ideal for Go developers looking for language-specific solutions, while fucking-algorithm offers a more language-agnostic approach with in-depth algorithmic insights.
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
Pros of LeetCodeAnimation
- Utilizes animations and visual aids to explain algorithms, making complex concepts easier to understand
- Covers a wide range of LeetCode problems, providing solutions for various difficulty levels
- Includes explanations in both Chinese and English, making it accessible to a broader audience
Cons of LeetCodeAnimation
- Less comprehensive in terms of algorithm theory and problem-solving strategies
- Animations may not be as effective for learners who prefer text-based explanations
- Updates are less frequent compared to fucking-algorithm
Code Comparison
LeetCodeAnimation:
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;
}
fucking-algorithm:
ListNode* reverseList(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}
ListNode* last = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return last;
}
Both repositories offer valuable resources for learning algorithms and solving LeetCode problems. LeetCodeAnimation excels in visual explanations, while fucking-algorithm provides more in-depth theoretical knowledge and problem-solving techniques.
:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计
Pros of CS-Notes
- Broader coverage of computer science topics, including operating systems, networks, and databases
- More comprehensive preparation for technical interviews and coding tests
- Includes system design concepts, which are crucial for senior-level positions
Cons of CS-Notes
- Less focus on in-depth algorithm explanations and problem-solving strategies
- May be overwhelming for beginners due to the vast amount of information covered
- Lacks the engaging narrative style found in fucking-algorithm
Code Comparison
CS-Notes example (Java):
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;
}
fucking-algorithm example (Python):
def reverseList(self, head: ListNode) -> ListNode:
prev, curr = None, head
while curr:
curr.next, prev, curr = prev, curr, curr.next
return prev
Both repositories offer valuable resources for learning algorithms and data structures. CS-Notes provides a more comprehensive overview of computer science topics, making it suitable for interview preparation and general knowledge. fucking-algorithm focuses more on in-depth algorithm explanations and problem-solving techniques, which can be particularly helpful for mastering specific algorithms and improving coding skills.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
labuladong Algo Notes
This repository contains 60+ original articles based on LeetCode problems, covering all problem types and techniques. The goal is to help you think algorithmically â not just memorize solutions.
When it comes to LeetCode, what matters is not the answer itself, but the thought process behind it. A repository full of raw code without explanation isn't very useful. The real value lies in understanding the frameworks and patterns that let you solve new problems on your own.
Most people grind LeetCode to land a job, not to compete in programming contests. So the focus here is on clarity and practical understanding â building reusable mental frameworks that make algorithm problems approachable and solvable.
Before You Start
1. Give this repo a star if you find it helpful â it keeps me motivated to write more.
2. I recommend studying on my website, where each article links to the corresponding LeetCode problems so you can read and practice side by side. The site covers 500+ problems with step-by-step guidance:
https://labuladong.online/en/algo/

Table of Contents
-
Getting Started: Data Structures and Sorting
-
Implement and Visualize 10 Sorting Algorithms
- Chapter Introduction
- Key Metrics of Sorting Algorithms
- Explore Selection Sort in Depth
- Bubble Sort with Stability
- Insertion Sort with Reverse Thinking
- Shell Sort - Better than O(N^2)
- Quick Sort and Binary Tree Preorder
- Merge Sort and Binary Tree Postorder
- Heap Sort and Binary Heap
- Counting Sort: A New Pespective on Sorting
- Bucket Sort
- Radix Sort
-
Chapter 0. Classic Problem Solving Templates
- Chapter Introduction
- How to Think About Data Structure and Algorithm
- Two Pointer Techniques for Linked List Problems
- Two Pointer Techniques for Array Problems
- Sliding Window Algorithm Code Template
- Thinking Recursion Algorithms from Binary Tree Perspective
- One Perspective + Two Thinking Patterns to Master Recursion
- Dynamic Programming Common Patterns and Code Template
- Backtracking Algorithm Common Patterns and Code Template
- BFS Algorithm Common Patterns and Code Template
- Backtracking Algorithm to Solve All Permutation/Combination/Subset Problems
- Greedy Algorithms Principles and Techniques
- Divide and Conquer Principles and Techniques
- Time and Space Complexity Analysis Practical Guide
-
Chapter 1. Data Structure Algorithms
-
- Two Pointer Techniques for Array Problems
- Match Three Game
- Tricks to Traverse a 2D Array
- Exercise: Two Pointer Techniques for Array
- Game of Life
- One Trick to Solve All N-Sum Problems
- Prefix Sum Array Technique
- Exercise: Prefix Sum Techniques
- Difference Array Technique
- Sliding Window Algorithm Code Template
- Exercise: Sliding Window In Action
- Sliding Window: Rabin Karp Algorithm
- Binary Search Algorithm Code Template
- Binary Search Follow-up
- Binary Search in Action
- Exercise: Binary Search Algorithm
- Weighted Random Selection Algorithm
- Advantage Shuffle Algorithm
-
- Implement Stack with Queue, Implement Queue with Stack
- Exercise: Stack Problems on LeetCode
- Exercise: Bracket Problems on LeetCode
- Exercise: Queue Problems on LeetCode
- Monotonic Stack Code Template
- Exercise: Monotonic Stack Problems on LeetCode
- Monotonic Queue to Solve Sliding Window Problems
- Exercise: Monotonic Queue Implementation and Leetcode Problems
-
- Thinking Recursion Algorithms from Binary Tree Perspective
- Binary Tree in Action (Traversal)
- Binary Tree in Action (Construction)
- Binary Tree in Action (Post-order)
- Binary Tree in Action (Serialization)
- Binary Search Tree in Action (In-order)
- Binary Search Tree in Action (Basic Operations)
- Binary Search Tree in Action (Construction)
- Binary Search Tree in Action (Post-order)
-
- Chapter Introduction
- Exercise: Binary Tree Traversal I
- Exercise: Binary Tree Traversal II
- Exercise: Binary Tree Traversal III
- Exercise: Binary Tree Divide and Conquer I
- Exercise: Binary Tree Divide and Conquer II
- Exercise: Binary Tree Combine Two Views
- Exercise: Binary Tree Post-order I
- Exercise: Binary Tree Post-order II
- Exercise: Binary Tree Post-order III
- Exercise: Binary Tree Level I
- Exercise: Binary Tree Level II
- Exercise: Binary Search Tree I
- Exercise: Binary Search Tree II
-
- Implementing LRU Cache like Building a Lego
- Implementing LFU Cache like Building a Lego
- How to Deleting Array Element in O(1) Time
- Exercise: Hash Table Problems on LeetCode
- Exercise: Priority Queue Problems on LeetCode
- Implementing TreeMap/TreeSet
- Basic Segment Tree Implementation
- Dynamic Segment Tree Implementation
- Lazy Update Segment Tree Implementation
- Exercise: Segment Tree Problems
- Implementing Trie Tree
- Exercise: Trie Problems on LeetCode
- Designing an Exam Room Algorithm
- Exercise: Classic Design Problems on LeetCode
- Implement Huffman Coding Compression
- Implement Consistent Hashing Algorithm
- How to Implement a Calculator
- Implementing Median Algorithm with Two Binary Heaps
- Removing Duplicates from an Array (Hard Version)
-
- How to Determine a Bipartite Graph
- Hierholzer Algorithm to Find Eulerian Path
- Exercise: Eulerian Path
- Cycle Detection Algorithm
- Topological Sort Algorithm
- Union-Find Algorithm
- Exercise: Union-Find Problems on LeetCode
- Dijkstra Algorithm
- Dijkstra Algorithm with Restrictions
- Exercise: Dijkstra Problems
- A* Algorithm
- Kruskal Minimum Spanning Tree Algorithm
- Prim Minimum Spanning Tree Algorithm
-
-
DFS and Backtracking Algorithm
- Backtracking Algorithm Common Patterns and Code Template
- Backtracking in Action: Sudoku and N-Queens
- Implement Sudoku Cheat
- Backtracking Algorithm to Solve All Permutation/Combination/Subset Problems
- Some Questions About Backtracking and DFS Algorithms
- Solve All Island Problems with DFS
- Minesweeper Game II
- Ball and Box: Two Perspectives of Backtracking Enumeration
- Backtracking Algorithm Practice: Generating Valid Parentheses
- Backtracking Algorithm Practice: Partitioning k Subsets
- Exercise: Backtracking Problems on LeetCode I
- Exercise: Backtracking Problems on LeetCode II
- Exercise: Backtracking Problems on LeetCode III
-
-
Chapter 3. Dynamic Programming Algorithms
-
- Dynamic Programming Common Patterns and Code Template
- How to Design Transition Equations
- How to Determine the Base Case and Initial Values for Memoization?
- Two Perspectives of Dynamic Programming Enumeration
- How to Convert Backtracking to Dynamic Programming
- Optimize Space Complexity for Dynamic Programming
- Clarifying Some Questions About Dynamic Programming
-
- Classic DP: Minimum Path Sum
- Play Dungeon Game with DP
- Play Freedom Trail with DP
- Save Money on Your Trip: Weighted Shortest Path
- Multi-source shortest path: Floyd algorithm
- Classic DP: Regular Expression Matching
- Classic DP: Egg Drop
- Classic DP: Burst Balloons
- Classic DP: Game Theory
- One Method to Solve All House Robber Problems on LeetCode
- One Method to Solve all Stock Problems on LeetCode
-
-
Chapter 4. Other Common Techniques
-
- LeetCode Problems with One Line Solution
- Common Bit Manipulation Techniques
- Essential Math Techniques
- Minesweeper Game I
- Random Algorithms in Games
- Two Classic Factorial Problems on LeetCode
- How to Efficiently Count Prime Numbers
- How to Find Missing and Duplicate Elements
- Interesting Probability Problems
- Exercise: Math Tricks
-
- How to Efficiently Solve the Trapping Rain Water Problem
- One Article to Solve All Ugly Number Problems on LeetCode
- One Method to Solve Three Interval Problems on LeetCode
- Split Array into Consecutive Subsequences
- Pancake Sorting Algorithm
- String Multiplication Calculation
- How to Determine if a Rectangle is Perfect
-
-
-
- Frontend Development Introduction for AI Era
- Introduction to Modern Encryption
- Understand Session and Cookie
- Understanding JSON Web Token (JWT)
- Authentication vs. Authorization
- Understanding OAuth 2.0 Authorization Framework
- OAuth 2.0 and OIDC Authentication
- OAuth 2.0 and PKCE
- Understanding Single Sign-On (SSO)
- Certificate and CA
- TLS Key Exchange
- Mutual TLS Authentication
- Introduction to Linux File System
- Linux Processes, Threads and File Descriptors
- Pitfalls of Linux Pipeline
- Linux Shell Tips
- LSM Tree in Storage System
- Updating
-
Top Related Projects
《Hello 算法》:动画图解、一键运行的数据结构与算法教程。支持简中、繁中、English、日本語,提供 Python, Java, C++, C, C#, JS, Go, Swift, Rust, Ruby, Kotlin, TS, Dart 等代码实现
《代码随想录》LeetCode 刷题攻略:200道经典题目刷题顺序,共60w字的详细图解,视频难点剖析,50余张思维导图,支持C++,Java,Python,Go,JavaScript等多语言版本,从此算法学习不再迷茫!🔥🔥 来看看,你会发现相见恨晚!🚀
LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)
✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot