Skip to content
yanghuafangPublic

About

Worked LeetCode problems in C++, kept as a study log rather than a submission archive. One standalone program per problem, each opening with the reasoning the code cannot show: the invariant it maintains, why a prune is sound, what it costs in time and space.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

84 Commits

Folders and files

Repository files navigation

leetcode

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.

Layout

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.

A solution file

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.cpp and array/binary_search_half_open.cpp
  • list/reverse_list_iterative.cpp and list/reverse_list_recursive.cpp
  • binary_tree/find_mode_one_pass.cpp and binary_tree/find_mode_inorder_array.cpp

Those pairs are alternatives worth contrasting, not a revision replacing an earlier attempt.

Building

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/solution

Every 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.

Commits

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.

About

Worked LeetCode problems in C++, kept as a study log rather than a submission archive. One standalone program per problem, each opening with the reasoning the code cannot show: the invariant it maintains, why a prune is sound, what it costs in time and space.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages