SkillAgentSearch skills...

Leetcode

Python & JAVA Solutions for Leetcode

Install / Use

/learn @qiyuangong/Leetcode
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode)

Remember solutions are only solutions to given problems. If you want full study checklist for code & whiteboard interview, please turn to jwasham's coding-interview-university.

Also, there are open source implementations for basic data structs and algorithms, such as Algorithms in Python and Algorithms in Java.

I'm currently working on Analytics-Zoo - an unified Data Analytics and AI platform. Check it out, if you are interested in big data and deep learning.

Problems & Solutions

Python and Java full list. ♥ means you need a subscription.

| # | Title | Solution | Basic idea (One line) | |---| ----- | -------- | --------------------- | | 1 | Two Sum | Python Java | 1. Hash O(n) and O(n) space.<br>2. Sort and search with two points O(n) and O(1) space. | | 2 | Add Two Numbers | Python Java | Take care of the carry from lower digit. | | 3 | Longest Substring Without Repeating Characters | Python Java |1. Check every possible substring O(n^2) <br>2. Remember the character index and current check pos, if character index >= current pos, then there is duplicate | | 4 | Median of Two Sorted Arrays | Python Java | 1. Merge two sorted lists and compute median, O(m + n) and O(m + n)<br>2. An extension of median of two sorted arrays of equal size problem| | 5 | Longest Palindromic Substring | Python Java | Background knowledge<br>1. DP if s[i]==s[j] and P[i+1, j-1] then P[i,j]<br>2. A palindrome can be expanded from its center<br>3. Manacher algorithm| | 7 | Reverse Integer | Python Java | Overflow when the result is greater than 2147483647 or less than -2147483648. | 8 | String to Integer (atoi) | Python Java | Overflow, Space, and negative number | | 9 | Palindrome Number | Python Java | Get the len and check left and right with 10^len, 10 | | 11 | Container With Most Water | Python Java | 1. Brute Force, O(n^2) and O(1)<br>2. Two points, O(n) and O(1) | | 12 | Integer to Roman | Python Java | Background knowledge Just like 10-digit number, divide and minus | | 13 | Roman to Integer | Python | Add all curr, if curr > prev, then need to subtract 2 * prev | | 15 | 3Sum | Python | 1. Sort and O(n^2) search with three points <br>2. Multiple Two Sum (Problem 1) | | 16 | 3Sum Closest | Python | Sort and Multiple Two Sum check abs| | 18 | 4Sum | Python | The same as 3Sum, but we can merge pairs with the same sum | | 19 | Remove Nth Node From End of List | Python Java | 1. Go through list and get length, then remove length-n, O(n) and O(n)<br>2. Two pointers, first pointer goes to n position, then move both pointers until reach tail, O(n) and O(n) | | 20 | Valid Parentheses | Python | 1. Stack<br>2. Replace all parentheses with '', if empty then True | | 21 | Merge Two Sorted Lists | Python | Add a dummy head, then merge two sorted list in O(m+n) | | 23 | Merge k Sorted Lists | Python | 1. Priority queue O(nk log k)<br>2. Binary merge O(nk log k)| | | 24 | Swap Nodes in Pairs | Python | Add a dummy and store the prev | | 28 | Implement strStr() | Python | 1. O(nm) comparison <br>2. KMP | | 35 | Search Insert Position | Python | Binary Search | | 46 | Permutations | Python | 1. Python itertools.permutations<br>2. DFS with swapping, O(n^2) and O(n^2)<br>3. iteratively generate n-permutations with (n-1)-permutations, O(n^3) and O(n^2) | | 47 | Permutations II | Python | 1. DFS with swapping, check duplicate, O(n^2) and O(n^2)<br>2. iteratively generate n-permutations with (n-1)-permutations, O(n^3) and O(n^2) | | 53 | Maximum Subarray | Python | 1. Recursion, O(nlgn), O(lgn)<br> 2. Bottom-up DP, O(n) and O(n)<br>3. Bottom-up DP, f(x) = max(f(x-1)+A[x], A[x]), f(x) = max(f(x-1)+A[x], A[x]),O(n) and O(1) | | 54 | Spiral Matrix | Python | O(nm) 1. Turn in the corner and loop<br>2. (0, 1) -> (1, 0) -> (0, -1) -> (-1, 0) | | 62 | Unique Paths | Python | 1. Bottom-up DP, dp[i][j] = dmap[i-1][j] + dmap[i][j-1], O(mn) and O(mn)<br>2. Combination (m+n-2, m-1) | | 63 | Unique Paths II | Python | Bottom-up DP, dp[i][j] = dmap[i-1][j] + dmap[i][j-1] (if block, then 0), O(mn) and O(mn) | | 65 | Valid Number | Python | 1. strip leading and tailing space, then check float using exception, check e using split <br>2. check is number split by . or e, note that number after e may be negative | | 66 | Plus One | Python | Check if current digit == 9. | | 70 | Climbing Stairs | [Python](http

View on GitHub
GitHub Stars5.3k
CategoryDevelopment
Updated21h ago
Forks1.5k

Languages

Python

Security Score

95/100

Audited on Mar 27, 2026

No findings