All solutions are explained in Explanation/README.md.
This is my personal repo while learning algorithms. There is NO GUARANTEE for opitimal solutions, so sometimes you might find a better one that what I post here (If so, I appreciate it that if you can post an issue).
LeetCode OJ is a platform for preparing technical coding interviews. Pick from an expanding library of hundreds of questions, code and submit your solution to see if you have solved it correctly.
You can visit Leetcode Online Judge for more information.
Each file in this repo goes with a number of simple tests, which use the example test testcase on LeetCode(or some cases made by myself).
You may utilize my test code (import _mytest
) to test the difference between your code and mine.
- Time testing is supported by line_profiler package.
- Unit testing is supported by unittest built-in library.
You may find that some problems are skipped, because I've planed to only cover easy and medium problems at first run. But don't worry, this is a long-term project. Just give me some time.
Because the number of Python 3 submission is always too few, I test all my Python 3 code under "Python" instead of "Python 3". Therefore, the rank and time is only for fun.
# | Title (with file link) | Cxty | Level | Category | Time | Rank | Note |
---|---|---|---|---|---|---|---|
1 | Two Sum | O(n) | Easy | HashTable | 32ms | 97.3% | |
2 | Add Two Numbers | O(n) | Medium | LinkedList | 149ms | 22.4% | |
3 | Longest Substring Without Repeating Characters | O(n) | Medium | HashTable | 82ms | 88.5% | |
5 | Longest Palindromic Substring | O(n) | Medium | String | 838ms | 76.9% | Manacher's Algo |
O(n^2) | 86ms | 95.3% | Odd & Even | ||||
6 | Zigzag Conversion | O(n) | Medium | String | 92ms | 82.5% | |
7 | Reverse Integer | O(1) | Easy | Math | 42ms | 94.4% | |
O(1) | 48ms | 72.0% | No num2str | ||||
8 | String to Integer (atoi) | O(1) | Medium | String | 58ms | 77.8% | |
9 | Palindrome Number | O(1) | Easy | Math | 346ms | 11.6% | O(1) Space |
O(n) | 182ms | 98.1% | O(n) Space | ||||
11 | Container With Most Water | O(n) | Medium | Optmz | 56ms | 95.2% | |
12 | Integer to Roman | O(n) | Medium | Math | 95ms | 94.5% | |
13 | Roman to Integer | O(n) | Easy | Math | 125ms | 82.8% | |
14 | Longest Common Prefix | O(nm) | Easy | String | 32ms | 96.4% | Pythonic |
O(nm) | 39ms | 66.7% | reduce function |
||||
15 | 3 Sum | O(n^2) | Medium | N/A | 959ms | 83.4% | Scan from both sides |
17 | Letter Combination of a Phone Number | O(n*4^n) | Medium | Backtrack | 32ms | 84.5% | |
18 | 4 Sum | O(n^3) | Medium | N/A | 99ms | 93.7% | N-Sum solution |
19 | Remove Nth Node From End of List | O(n) | Medium | LinkedList | 39ms | 73.0% | |
20 | Valid Parenthesis | O(n) | Easy | Stack | 32ms | 95.1% |