This repository serves as a robust and automated environment for solving LeetCode problems using C++ and CMake, with integrated GoogleTest for comprehensive testing.
- Automated Test Discovery: Simply add your
solution.cppfile for a problem, and the build system automatically discovers and runs its tests. - Concise Test Output: Get clear, summarized results for passing tests, with detailed output only for failures.
- Performance Metrics: Each test run reports execution time and memory usage (VmRSS on Linux).
- Standardized Structure: Follows a clean, scalable project layout for easy navigation and maintenance.
- GoogleTest Integration: GoogleTest is automatically fetched and configured by CMake, no manual installation required.
- A C++ compiler (e.g., GCC, Clang) that supports C++17.
- CMake (version 3.14 or higher).
- Git.
-
Clone the repository:
git clone https://github.com/Suyash8/dsa-leetcode.git cd dsa-leetcode -
Build the project: The first build will download GoogleTest and configure the project.
./run_tests.sh
This script will handle
cmake -S . -B build,cmake --build build, andctest -Vfor you, including retries if the build directory needs to be recreated.
-
Create a new directory for the problem: In the
src/directory, create a new subdirectory for your problem. The name of this directory will be used as the identifier for the test inctest.Design Choice: You can name this directory anything you like (e.g.,
0001,0001-two-sum,my-first-problem). However, it's highly recommended to include the LeetCode problem number (e.g.,src/0001/orsrc/0001-two-sum/) for clarity and easy identification.Example:
mkdir src/1108-defanging-an-ip-address # or simply: # mkdir src/1108
-
Create
solution.cpp: Inside the new directory (src/N/orsrc/N-problem-name/), create a file namedsolution.cpp.touch src/1108-defanging-an-ip-address/solution.cpp
-
Populate
solution.cpp: Yoursolution.cppfile must follow this structure:- Includes: Add any necessary headers for your solution.
- Problem-Specific Data Structures: Define any custom data structures (e.g.,
ListNode,TreeNode) required by the problem. - Helper Functions (Optional): Include helper functions to convert between problem-specific data structures and standard C++ containers for easier test case definition (e.g.,
createList,listToVector). Input,Output,TestCaseType Aliases: DefineInputandOutputtypes based on the LeetCode problem's function signature. Usestd::tuplefor multiple input parameters.TestCaseshould bestd::pair<Input, Output>.test_casesVector: Populate astatic const std::vector<TestCase> test_caseswith your input/expected output pairs.SolutionClass:- Implement the original LeetCode method (e.g.,
defangIPaddrfor problem 1108). - Implement a
Output solve(Input input)wrapper method. This method will unpack theInputtuple and call your original LeetCode method.
- Implement the original LeetCode method (e.g.,
// Example structure for src/N/solution.cpp #include <vector> #include <string> #include <tuple> // ... other includes // Problem-specific data structures (if any) // struct ListNode { ... }; // Helper functions for test cases (if any) // ListNode* createList(const std::vector<int>& vals) { ... } // Test Case Type Definitions using Input = std::string; // Adjust based on problem using Output = std::string; // Adjust based on problem using TestCase = std::pair<Input, Output>; // Test Cases static const std::vector<TestCase> test_cases = { {"1.1.1.1", "1[.]1[.]1[.]1"}, // ... more test cases }; class Solution { public: // Original LeetCode method signature std::string defangIPaddr(std::string address) { // Your solution logic std::string result = ""; for (char c : address) { if (c == '.') { result += "[.]"; } else { result += c; } } return result; } // Wrapper method for generic test runner Output solve(Input input) { // Unpack input if it's a tuple, then call your LeetCode method return defangIPaddr(input); } };
-
Run Tests: Execute the
run_tests.shscript from the root of your repository../run_tests.sh
The system will automatically detect your new solution, compile it, and run all its test cases, providing concise output.
It's recommended to follow a feature branch workflow for each problem:
git checkout developgit checkout -b feat/N-problem-name(e.g.,feat/0002-add-two-numbers)- Implement your solution and test cases in
src/N/solution.cpp. - Run
./run_tests.shto verify. git add src/N/solution.cppgit commit -m "feat(N): solve Problem Name"git checkout developgit merge --no-ff feat/N-problem-namegit branch -d feat/N-problem-namegit push origin develop(orgit push -u origin developfor the first time)
This project is licensed under the MIT License - see the LICENSE file for details.