Skip to content

ethanh6/LeetCode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Leetcode solution tracker

This is a Leetcode solutions tracker built with Python that automatically retrieves code snippets from the Leetcode API. It comes with a preconfigured code template that includes essential C++ headers and a Catch2 unit testing boilerplate to help verify code correctness. CMake is utilized to streamline the building and testing process for ease of use.

This problem set is based on Grind 169.

Environment

How to use

0. Create and activate virtual env and install dependencies

virtualenv leetcode_env
source leetcode_env/bin/activate
pip3 install -r requirements.txt

1. Add question

python3 scripts/add_question.py

Enter the question url after prompt e.g. https://leetcode.com/problems/remove-element/

Corresponding C++ template file will be created as src/<question_id>-<question_slug>.cpp.

CMakeLists.txt will also be updated.

If the question already exists, the script will prompt that it exists and does nothing to the file structures.

The template:

#include "leetcode.hpp"

class Solution {
public:
    int func() {
        return 123;
    }
};

TEST_CASE("[question slug]", "[question id]"){
    Solution sol;
    CHECK(sol.func() == 123);
}

2. Think and code

question_id is 4 digit number, front-padded with zeros.

./src/<question_id>.cpp

3. Build project

cmake -S . -B build

4. Build all targets

cmake --build build

or build only single target (this will only build ./build/<question_id>)

cmake --build build --target <questioni_id>

5. Execute your solution

./build/<question_id>

Or run with keymap
<leader>make

-- this will be mapped to the following commands.
-- strpart(expand('%:t') retrieves the 4 digit question id, which is the target name.

:execute '!make -C build ' . strpart(expand('%:t'), 0, 4)<cr>:execute '!./build/' . strpart(expand('%:t'), 0, 4)<cr>:make -C build clean<cr>

For instance, say the current buffer is "src/0001-two-sum.cpp", the script would be interpreted as

make -C build 0001 && ./build/0001 && make -C build clean

A single script to build -> run -> clean:

cmake --build build --target <question_id>; ./build/<question_id>; cmake --build build --target clean

Reference: My Neovim config