This repo stores solutions to questions on LeetCode that I did for practice. If you are only looking for "How to Run" instructions please feel free to skip ahead to the How to run code section. Original questions, examples and constraints can be found on the LeetCode website. Solutions based on other people's solutions are cited in the same file usually at the top of the function in a comment.
I have created a top level folder for each language that I have solutions for. Then under each language there are some helper files to provide commonly reused code, these are liable to change as I discover more of what is common among LeetCode problems. Other than the helper files there is one file per problem in the folder. I tried to keep naming of the files obvious. Python and Rust do not allow identifiers to start with a number, so filenames start with an underscore.
I've tried to keep it as simple as possible but also wanted to minimize required repeated work, as a result some steps
are designed with IDE support in mind, like dependency importing (works in PyCharm but auto complete didn't work for me
in VS Code). All python commands are expected to be run from the root folder of the repository where "README.md" is
found. Rust commands are expected to be run from inside the rust folder where the Cargo.toml
is.
Language:
Assumption: Both pip and python 3.6 or greater are already installed.
- Install dependencies:
pip install -r requirements.txt
- Link main.py to problem
- Uncomment "tester" function call
- Add import for tester from desired problem e.g.
from python3._1_TwoSum import tester
- Execute run.py
python3 run.py
Should show output that it was run and how long it took
Assumption: Latest version of Rust is installed. Should still work with fairly old versions as leet code doesn't upgrade their rust version often.
- Link lib.rs to problem (Add file as module. eg
mod _1_two_sum;
). - Run tests
cargo test
Should show output that it was run and how long it took
Language
- Create file for problem in python3 folder
- Copy starter code from LeetCode into file (optionally add pass to clear errors)
- Copy starter tester code into newly created file from main (can be identified as commented out as a string at bottom of the file)
- Import helper library
- Fill in starter code with problem number and other required parameters (parameters can be seen in helper file).
- Fill in example test cases
- Some problems may require input transformation, simply call a function to transform the input so that your solution gets the input in the right format e.g. convert an integer list to tree as seen in problem 1302
- Some problems allow multiple solutions those often need to be handled on a case by case basis but in general if the output is an unordered list you can find an example in problem 47
- Now you can implement your solution and test and debug locally.
- Create file for problem in rust folder
- Setup Starter Code and get it able to be compiled and prepare tests (Copy template from bottom of lib.rs or follow
steps below)
- Copy starter code from LeetCode into file.
- Add the required Solution unit struct
struct Solution;
- Add
todo!()
in function body. - Create a test module at the bottom of the file for the tests
- Create a test for each example. I usually call them Case1, Case2, Case3 and so on.
- Some problems may require input transformation, transformations for implemented types usually have a wrapper type and then an into from that wrapper type see example in problem 2 for linked list or problem 100 for Tree.
- Some problems allow multiple solutions. I haven't done any of those yet but should be evaluated using a custom evaluation method.
- Now you can implement your solution and test and debug locally.