Fast C++ solutions for leetcode.com problems, with VS Code support for CMake
building, gdb
/lldb
debugging, and Boost
unit testing.
99.00%+ in running time: 105 / 159
- Hash table (unorderd_map): 4ms (top 99.54%)
- Linked list: 16ms (top 100.00%)
- Hash table (vector): 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- Lookup table: 52ms (top 100.00%)
- Dynamic programming: 4ms (top 100.00%)
- Two pointers: 12ms (top 100.00%)
- 4ms (top 100.00%)
- Two pointers: 112ms (top 99.88%)
- Two pointers: 8ms (top 100.00%)
- 0ms (top 100.00%)
- Stack: 4ms (top 100.00%)
- Two swaps: 12ms (top 100.00%)
- 0ms (top 100.00%)
- 20ms (top 95.79%)
- 20ms (top 99.75%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 35ms (top 94.13%)
- 4ms (top 96.34%)
- 4ms (top 96.65%)
- 0ms (top 100.00%)
- 8ms (top 98.52%)
- 4ms (top 98.31%)
- 8ms (top 99.96%)
- 4ms (top 95.52%)
- 4ms (top 97.84%)
- 8ms (top 95.15%)
- 4ms (top 98.92%)
- 4ms (top 96.34%)
- 4ms (top 99.71%)
- 4ms (top 99.84%)
- 8ms (top 95.32%)
- 8ms (top 96.44%)
- 40ms (top 95.50%)
- 4ms (top 96.07%)
- 4ms (top 99.62%)
- 0ms (top 100.00%)
- 28ms (top 99.98%)
- 8ms (top 100.00%)
- 0ms (top 100.00%)
- 4ms (top 94.70%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 4ms (top 99.39%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 44ms (top 98.55%)
- 4ms (top 99.56%)
- sliding window: 32ms (top 38.25%)
- optimized sliding window: 44ms (top 19.33%)
- hash: 4ms (top 99.92%)
- 3ms (top 92.82%)
- 7ms (top 62.59%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- Recursion - list: 4ms (top 87.58%)
- Recursion - vector: 4ms (top 43.93%)
- Stack: 0ms (top 100.00%)
- Stack: 0ms (top 100.00%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 3ms (top 73.66%)
- 8ms (top 75.97%)
- 23ms (top 57.38%)
- 4ms (top 99.83%)
- 4ms (top 99.83%)
- 4ms (top 89.52%)
- 11ms (top 99.39%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 68ms (top 99.95%)
- 16ms (top 99.85%)
- 4ms (top 99.16%)
- 28ms (top 99.56%)
- Graph + BFS: 138ms (top 67.54%)
- Graph + BiDir BFS: 131ms (top 69.50%)
- BiDir BFS: 560ms (top 34.48%)
- 4ms (top 99.88%)
- 0ms (top 100.00%)
- 4ms (top 99.88%)
- 3ms (top 99.91%)
- 3ms (top 99.53%)
- 12ms (top 91.72%)
- 0ms (top 100.00%)
- 28ms (top 99.98%)
- 8ms (top 76.49%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 4ms (top 99.81%)
- 8ms (top 99.64%)
- 0ms (top 100.00%)
- 7ms (top 99.74%)
- 276ms (top 60.58%)
- 24ms (top 97.70%)
- 7ms (top 99.85%)
- 168ms (top 99.89%)
- 84ms (top 72.87%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 12ms (top 68.31%)
- 0ms (top 100.00%)
- 64ms (top 86.51%)
- 44ms (top 13.37%)
- 4ms (top 99.54%)
- 27ms (top 41.24%)
- 40ms (top 65.32%)
- 853ms (top 36.82%)
- 0ms (top 100.00%)
- 20ms (top 100.00%)
- 7ms (top 89.75%)
- 0ms (top 100.00%)
- 11ms (top 56.65%)
- 28ms (top 96.32%)
- 64ms (top 98.82%)
- 0ms (top 100.00%)
- 32ms (top 98.21%)
- 72ms (top 99.48%)
- 20ms (top 99.82%)
- 60ms (top 99.69%)
- BFS: 180ms (top 81.07%)
- 4ms (top 88.29%)
- 24ms (top 96.91%)
- 179ms (top 16.32%)
- 0ms (top 100.00%)
- 8ms (top 89.88%)
- 3ms (top 84.99%)
- 0ms (top 100.00%)
- 136ms (top 99.88%)
Category | Problems |
---|---|
Array / String | 1, 3, 6, 11, 14, 15, 16, 18, 20, 22, 26, 27, 28, 30, 31, 38, 41, 42, 44, 45, 48, 49, 54, 55, 56, 73, 75, 76, 80, 88, 125, 239, 283, 349, 350, 438, 567, 670, 832, 1838 |
Linked list | 2, 19, 21, 23, 24, 25, 61, 86, 92, 141, 142, 160, 206, 234, 876 |
Recursion | 4, 33, 34, 35, 69, 74, 153, 215, 240, 278, 287, 301, 540, 704 |
Dynamic Programming | 5, 10, 32, 36, 37, 53, 62, 63, 64, 70, 79, 84, 91, 96, 118, 120, 121, 122, 123, 128, 130, 134, 188, 198, 213, 221, 279, 300, 309, 322, 509, 714, 864, 871 |
Numerical | 7, 8, 9, 12, 13, 29, 43, 50, 66, 136, 150, 564 |
Backtracking | 17, 39, 40, 46, 47, 51, 60, 78, 131 |
Binary Tree | 94, 98, 101, 102, 103, 104, 105, 106, 108, 111, 114, 116, 124, 144, 145, 226, 230, 235, 236, 337, 450, 538, 543, 652, 654, 700, 701, 889, 958, 1038 |
Graph / DFS | 127, 752 |
System Design | 146, 460 |
- Ubuntu 21.10 / Manjaro 19.0 / Windows 10 / macOS Catalina 10.15.3
- Visual Studio Code with following extensions:
- C/C++ (ms-vscode.cpptools)
- C/C++ Clang Command Adapter (mitaki28.vscode-clang) (optional)
- CMake (twxs.cmake)
- CMake Tools (vector-of-bool.cmake-tools)
- CodeLLDB (vadimcn.vscode-lldb) (optional)
sudo snap install --classic code
sudo apt-get install pylint
- clang (used by default. You can use other tools as long as your
.vscode/c_cpp_properties.json
file is modified correctly)
wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
sudo apt-get update
sudo apt-get install clang-8 lldb-8
- CMake
- Boost (for unit testing)
wget -O boost_1_70_0.tar.gz https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
tar xzvf boost_1_70_0.tar.gz
cd boost_1_70_0/
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev
./bootstrap.sh --prefix=/usr/
./b2
sudo ./b2 install
Each folder contains 3 files: solution.hpp
, solution.cpp
and solution_test.cpp
. The first folder 001-Two-Sum
contains an additional main.cpp
file, in case you don't feel like using unit testing and you can test your own cases in main.cpp
.
Add following lines to your settings.json
to configure the building and linting:
{
"clang.executable": "clang++",
"clang.cflags": ["c11"],
"clang.cxxflags": ["-std=c++17"],
"lldb.executable": "lldb"
}
Steps to build a solution:
-
Change the line
set(PROBLEM_NAME {Problem_Folder})
inCMakeLists.txt
to choose the problem you want to solve, in which{Problem_Folder}
is the folder name of the problem. For example,set(PROBLEM_NAME "001-Two-Sum")
. -
Press
Ctrl
+Shift
+P
to bring up the Command Palette of VSCode. Type inCMake
, and look for aCMake: Configure
command, select it. It will configure the cache files and makefile which are located inbuild
folder by default. -
Type in or look for a
CMake: Build
command in the Command Palette and execute it. It will compile the source codes of the solution that you previously chose. -
Debug Press
Ctrl
+Shift
+D
or click the bug icon on the left. The.vscode/launch.json
for debugging is already set up for you, and both GDB and LLDB is supported.gdb: Choose
(gdb) Launch
and you're good to go.lldb: Choose
(lldb) Launch
. You may need to disable and re-enablems-vscode.cpptools
andmitaki28.vscode-clang
extensions in case the variables won't properly display in "Watch" Panel. Data visualization is supported (https://github.com/vadimcn/vscode-lldb/wiki/Data-visualization). -
Unit testing Type in or look for a
CMake: Run tests
command in the Command Palette and execute it. It will run all test cases insolution_test.cpp
. Feel free to add your own test cases.
Clear build folder:
- Press
Ctrl
+Shift
+P
to bring up the Command Palette. Type inTask: Run Task
and pressenter
. Chooseclean
will delete everything in thebuild
folder. Bear in mind that you need to configure the project after cleaning the folder or switching to another solution, by either executingTask: Run Task
->configure
orCMake: Configure
.