Worked LeetCode problems in C++, kept as a study log rather than a submission archive. The substance of a file here is the reasoning above the code — the invariant a loop maintains, the property that makes a prune sound, the reason one traversal order is required where another is not. Anything a reader could work out from the code itself is left unwritten.
One directory per topic, in the order the topics are worked through.
| Topic | Directory |
|---|---|
| Array | array/ |
| Linked list | list/ |
| Hash table | hash/ |
| String | string/ |
| Stack and queue | stack/ |
| Binary tree | binary_tree/ |
| Backtracking | backtracking/ |
| Greedy | greedy/ |
| Dynamic programming | dynamic_programming/ |
| Monotonic stack | monotonic_stack/ |
| Graph theory | graph/ |
A directory appears with the first problem worked in that topic. Monotonic stack comes after backtracking rather than beside the other stack problems because it is a technique rather than a data structure, and reads better once the plain stack problems are familiar.
Every file is a standalone program: one problem, its solution, and a main
that exercises it. Nothing is shared — no common header, no build system — so
any file can be read, compiled and run on its own, and a ListNode or
TreeNode that several problems need is redeclared in each.
Each file opens with a comment block giving the problem number, its URL, what the problem asks, and why the approach works. Comments inside the code are reserved for the parts that are genuinely counterintuitive; the rest is left to speak for itself.
Files are named after the problem rather than its number — the number is on the first line of the file. Where one problem is solved twice, the file names say which approach each holds, and each header names the other:
array/binary_search_closed.cppandarray/binary_search_half_open.cpplist/reverse_list_iterative.cppandlist/reverse_list_recursive.cppbinary_tree/find_mode_one_pass.cppandbinary_tree/find_mode_inorder_array.cpp
Those pairs are alternatives worth contrasting, not a revision replacing an earlier attempt.
There is nothing to configure. Compile whichever file you want to read:
clang++ -std=c++17 -Wall -Wextra -o /tmp/solution array/binary_search_closed.cpp
/tmp/solutionEvery file is warning-free at -Wall -Wextra. Most assert their expected
results, so one that runs to completion has passed its own tests; the rest
print for inspection.
The code follows the Google C++ Style Guide: PascalCase functions,
snake_case variables, k-prefixed constants, and formatting pinned at 80
columns in .clang-format.
One problem per commit. The subject carries the problem number and the strategy; the body explains what the diff does not make obvious, which is usually the failure a particular choice avoids. Two approaches to one problem are two commits.