Personal notes for leetcode.com solutions with support for local building and unit testing.
Top interviewed: 64 / 145
Appear in nowcoder.com: 2 / 148
In total: 70 / 1350
Solutions that are 99.00%+ in running time: 43
- Linked list: 16ms (top 100.00%)
- Hash table (map): 8ms (top 100.00%)
- Use
INT_MAX: 4ms (top 100.00%)
- Use
INT_MAX: 8ms (top 99.90%)
- 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%)
- Deque: 4ms (top 100.00%)
- One pass: 8ms (top 100.00%)
- Stack: 4ms (top 100.00%)
- Two swaps: 12ms (top 100.00%)
- Deque: 8ms (top 100.00%)
- 20ms (top 99.75%)
- All
intwithout<limits.h>: 8ms (top 100.00%)
- 0ms (top 100.00%)
- 8ms (top 98.52%)
- 8ms (top 99.96%)
- 4ms (top 97.84%)
- 4ms (top 96.34%)
- 4ms (top 99.71%)
- 4ms (top 99.84%)
- 40ms (top 95.50%)
- 4ms (top 96.07%)
- 0ms (top 100.00%)
- 4ms (top 99.99%)
- 8ms (top 100.00%)
- 4ms (top 93.30%)
- 0ms (top 100.00%)
- 0ms (top 100.00%)
- 4ms (top 90.67%)
- 44ms (top 98.55%)
- 0ms (top 100.00%)
- sliding window: 32ms (top 38.25%)
- optimized sliding window: 44ms (top 19.33%)
- hash: 4ms (top 99.92%)
- 4ms (top 90.46%)
- 4ms (top 99.99%)
- 0ms (top 100.00%)
- 4ms (top 89.75%)
- 12ms (top 61.51%)
- 24ms (top 80.31%)
- 4ms (top 99.83%)
- 16ms (top 95.40%)
- 0ms (top 100.00%)
- 16ms (top 99.85%)
- 4ms (top 99.16%)
- 32ms (top 94.91%)
- 12ms (top 91.72%)
- Ubuntu 18.04
- Visual Studio Code with following extensions:
- C/C++ (ms-vscode.cpptools)
- C/C++ Clang Command Adapter (mitaki28.vscode-clang)
- CMake (twxs.cmake)
- CMake Tools (vector-of-bool.cmake-tools)
- CodeLLDB (vadimcn.vscode-lldb)
sudo snap install --classic code sudo apt-get install pylint
- clang (clang++-8 is used by default. You can use other tools as long as your
.vscode/c_cpp_properties.jsonfile 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 installEach 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++-8",
"clang.cflags": ["c11"],
"clang.cxxflags": ["-std=c++14"],
"lldb.executable": "lldb-8"
}Steps to build a solution:
-
Change the line
set(PROBLEM_NAME {Problem_Folder})inCMakeLists.txtto 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+Pto bring up the Command Palette of VSCode. Type inCMake, and look for aCMake: Configurecommand, select it. It will configure the cache files and makefile which are located inbuildfolder by default. -
Type in or look for a
CMake: Buildcommand in the Command Palette and execute it. It will compile the source codes of the solution that you previously chose. -
Debug Press
Ctrl+Shift+Dor click the bug icon on the left. The.vscode/launch.jsonfor debugging is already set up for you, and both GDB and LLDB is supported.gdb: Choose
(gdb) Launchand you're good to go.lldb: Choose
(lldb) Launch. You may need to disable and re-enablems-vscode.cpptoolsandmitaki28.vscode-clangextensions 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 testscommand 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+Pto bring up the Command Palette. Type inTask: Run Taskand pressenter. Choosecleanwill delete everything in thebuildfolder. Bear in mind that you need to configure the project after cleaning the folder or switching to another solution, by either executingTask: Run Task->configureorCMake: Configure.