Leetcode
Solutions to LeetCode problems; updated daily. Subscribe to my YouTube channel for more.
Top Related Projects
Provide all my solutions and explanations in Chinese for all the Leetcode coding problems.
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
刷算法全靠套路,认准 labuladong 就够了!English version supported! Crack LeetCode, not only how, but also why.
Quick Overview
The fishercoder1534/Leetcode repository is a comprehensive collection of LeetCode problem solutions implemented in Java. It serves as a valuable resource for developers preparing for technical interviews or looking to improve their algorithmic problem-solving skills. The repository contains well-organized and commented solutions for a wide range of LeetCode problems.
Pros
- Extensive coverage of LeetCode problems with Java solutions
- Well-organized structure with clear problem categorization
- Detailed comments and explanations for each solution
- Regular updates and contributions from the community
Cons
- Primarily focused on Java, limiting language diversity
- Some solutions may not be optimized for performance
- Lack of comprehensive test cases for each problem
- May encourage over-reliance on solutions instead of independent problem-solving
Code Examples
- Two Sum problem 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");
}
This code solves the Two Sum problem using a HashMap to efficiently find the complement of each number.
- Reverse Linked List 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;
}
This code reverses a singly linked list iteratively by changing the next pointers of each node.
- Binary Tree Inorder Traversal solution:
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderHelper(root, result);
return result;
}
private void inorderHelper(TreeNode node, List<Integer> result) {
if (node != null) {
inorderHelper(node.left, result);
result.add(node.val);
inorderHelper(node.right, result);
}
}
This code performs an inorder traversal of a binary tree recursively, adding node values to a list.
Getting Started
To use the solutions in this repository:
-
Clone the repository:
git clone https://github.com/fishercoder1534/Leetcode.git
-
Navigate to the desired problem solution in the appropriate package.
-
Copy the solution code and adapt it to your needs or use it as a reference for solving similar problems.
-
Make sure to understand the solution thoroughly before using it in interviews or coding assessments.
Competitor Comparisons
Provide all my solutions and explanations in Chinese for all the Leetcode coding problems.
Pros of Leetcode (grandyang)
- More comprehensive coverage with solutions to over 1000 LeetCode problems
- Detailed explanations and multiple approaches for many problems
- Solutions provided in C++, which may be preferred by some users
Cons of Leetcode (grandyang)
- Less frequently updated compared to Leetcode (fishercoder1534)
- Primarily focuses on C++ solutions, limiting language diversity
- May lack some of the newer LeetCode problems
Code Comparison
Leetcode (grandyang) - C++ solution for Two Sum:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
if (m.count(target - nums[i])) return {m[target - nums[i]], i};
m[nums[i]] = i;
}
return {};
}
Leetcode (fishercoder1534) - Java solution for Two Sum:
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) return new int[]{map.get(target - nums[i]), i};
map.put(nums[i], i);
}
return new int[]{};
}
Both repositories offer valuable resources for LeetCode problem-solving, with grandyang's repo providing more extensive coverage in C++, while fishercoder1534's repo offers solutions in Java and is more frequently updated.
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
Pros of LeetCodeAnimation
- Provides visual animations for algorithm explanations, making concepts easier to understand
- Covers a wide range of LeetCode problems with detailed explanations
- Includes solutions in multiple programming languages
Cons of LeetCodeAnimation
- Less frequently updated compared to Leetcode
- Focuses more on explanations and animations rather than providing extensive code solutions
- May not cover as many problems as Leetcode
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 (Java):
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 LeetCode problem-solving. LeetCodeAnimation excels in visual explanations and multi-language support, while Leetcode provides a more extensive collection of problems and solutions with frequent updates. The choice between the two depends on individual learning preferences and specific needs for LeetCode preparation.
✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
Pros of LeetCode-Go
- Written in Go, offering solutions for those learning or working with the language
- Includes detailed explanations and complexity analysis for each solution
- Provides multiple approaches for many problems, showcasing different algorithms and techniques
Cons of LeetCode-Go
- Focuses solely on Go, limiting its usefulness for developers working in other languages
- May have fewer total problems solved compared to Leetcode
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 (Java):
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[]{map.get(target - nums[i]), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
Both repositories offer high-quality LeetCode solutions, with LeetCode-Go specializing in Go implementations and Leetcode providing a broader range of language solutions, primarily in Java. The choice between them depends on the user's preferred programming language and learning goals.
刷算法全靠套路,认准 labuladong 就够了!English version supported! Crack LeetCode, not only how, but also why.
Pros of fucking-algorithm
- Provides in-depth explanations and analysis of algorithms
- Covers a wide range of topics beyond just LeetCode problems
- Offers a structured learning approach with categorized content
Cons of fucking-algorithm
- Less frequently updated compared to Leetcode
- Primarily written in Chinese, which may be a barrier for non-Chinese speakers
- Focuses more on concepts and less on specific problem solutions
Code Comparison
Leetcode (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 (Python):
def reverseList(self, head: ListNode) -> ListNode:
pre, cur = None, head
while cur:
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
return pre
Both repositories provide solutions to the "Reverse Linked List" problem, but fucking-algorithm typically includes more detailed explanations and analysis of the algorithm's time and space complexity, as well as potential variations and optimizations.
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
LeetCode
If you like this project, please leave me a star. ★
Algorithms
Database
Shell
Javascript
Contributing
Your ideas/fixes/algorithms are more than welcome!
- Please make sure your PR builds after submitting! Check out here: https://travis-ci.org/github/fishercoder1534/Leetcode/pull_requests and look for your PR build.
- Fork this repo
- Clone your forked repo (
git clone https://github.com/YOUR_GITHUB_USERNAME/Leetcode.git
) onto your local machine cd
into your cloned directory, create your feature branch (git checkout -b my-awesome-fix
)git add
your desired changes to this repo- Commit your changes (
git commit -m 'Added some awesome features/fixes'
) - Push to the branch (
git push origin my-awesome-feature
) - Open your forked repo on Github website, create a new Pull Request to this repo!
Best way to open this project
- Install IntelliJ on your machine, either CE or UE.
- git clone this repo to your local disk
- import this project as a new project (does need to be imported as a gradle project)
- If you run into "Could not determine Java version using executable ..." error, use local gradle distribution: "/usr/local/Cellar/gradle/4.8.1/libexec/" instead of the default one. More details, see this question on Stackoverflow.
Top Related Projects
Provide all my solutions and explanations in Chinese for all the Leetcode coding problems.
Demonstrate all the questions on LeetCode in the form of animation.(用动画的形式呈现解LeetCode题目的思路)
✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
刷算法全靠套路,认准 labuladong 就够了!English version supported! Crack LeetCode, not only how, but also why.
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