This file provides guidance to AI coding assistants when working with code in this repository.
This is doocs/leetcode — a large collection of LeetCode, Coding Interviews, and other algorithm problem solutions, each implemented in multiple programming languages (Python, Java, C++, Go, TypeScript, Rust, C#, PHP, JavaScript, Kotlin, Swift, Scala, Ruby, Nim, Shell, SQL).
-
solution/— Main LeetCode solutions, organized by problem number ranges (e.g.,0000-0099/,0100-0199/). Each problem has its own directory (e.g.,0000-0099/0001.Two Sum/) containing:README.md/README_EN.md— Chinese/English problem descriptionsSolution.{py,java,cpp,go,ts,cs,rs,...}— Solution files in each language- Follow standard LeetCode class-based structure:
class Solutionwith the method
-
lcof/— Coding Interviews (2nd Edition); problem directories use Chinese titles -
lcof2/— Coding Interviews (Special Edition) -
lcci/— Cracking the Coding Interview (6th Edition) —01.01.Is Uniqueformat -
lcp/— LeetCode Contest Problems -
lcs/— LeetCode Contest (separate series) -
basic/— Basic algorithm implementations (sorting algorithms like BubbleSort, QuickSort, etc.)
main— problem sources, lint tooling, and deploy workflows.docs— MkDocs site engine only (build_site.py,mkdocs.yml,hooks/,overrides/). Problem pages are generated at deploy time frommain.
Deploy checks out both branches, overlays a whitelist from docs onto main, runs python3 build_site.py, then builds zh/en in parallel. Content pushes on main (problem trees, worker.js, and wrangler.jsonc) go through deploy-request.yml (about 90s quiet period, then gh workflow run deploy.yml). Pushes to docs use .github/workflows/trigger-deploy.yml on the docs branch the same way. A started deploy.yml run is not cancelled. Gitee Pages publish is a separate publish-gitee.yml workflow dispatched after the site build, so a slow Gitee push does not hold the deploy-site concurrency group. Pushes to main run sync-gitee.yml to force-push that branch to Gitee; pushes to docs run trigger-sync-gitee.yml on the docs branch, which dispatches the same sync for docs. Branch mirroring uses its own concurrency group and does not queue site deploys.
Dependabot updates npm, GitHub Actions, and pip on main, and pip on docs.
- Create a new problem directory under the appropriate parent (e.g.,
solution/0000-0099/0042.My Problem/) - Add
Solution.py,Solution.java,Solution.cpp,Solution.go,Solution.ts,Solution.rs,Solution.cs, etc. - Add
README.mdandREADME_EN.mdfromsolution/template.md(problem statement, methods, complexity, code tabs) - Optionally add a Thinking block after a method heading (see Thinking section). READMEs may omit it.
- All language solutions must implement the same algorithm logic
- Open the PR with
.github/pull_request_template.mdand complete the checklist (see alsoCONTRIBUTING.md)
All code must be formatted before committing. The lint-staged hooks run automatically on pre-commit:
# JavaScript/TypeScript/PHP/SQL/Markdown
pnpm exec prettier --write "**/*.{js,ts,php,sql,md}"
# Python
node scripts/run-py.js -m black -S <file>
# C/C++/Java (LLVM 23 via pip clang-format)
node scripts/run-py.js run_format.py --clang-format <file>
# Go (adds/removes the `package main` header used by Solution.go files)
node scripts/run-py.js run_format.py --gofmt <file>
# Rust
rustfmt --edition 2021 <file>Or run the full formatting script:
node scripts/run-py.js run_format.pypnpm install # Node/pnpm devDependencies (uses the committed lockfile)
pnpm run setup:python # Optional: black and the problem-sync spiderGitHub Actions automatically run:
- clang-format lint on changed C/C++/Java files (LLVM 23, same pip package as local)
- Black lint on changed Python files
- gofmt lint on changed Go files
- rustfmt lint on changed Rust files
- Prettier on JS/TS/PHP/SQL/Markdown files (auto-format same-repo PRs to
main;--checkon all PRs) - thinking-check on changed
README.md/README_EN.md: existing Thinking blocks must be non-empty and not outnumber method headings. Missing blocks are allowed. - Deploy as described under Branch model. Same-repo Prettier uses
pull_request_targetand skips forks so it never installs untrustedpackage.json.
- Python solutions use the builtin
list(for examplelist[int]) - Go solutions include
package mainheader (added/removed by formatting script) - PHP solutions include
<?phpheader (added/removed by formatting script) - SQL solutions uppercase built-in function names (handled by
run_format.py) - C# solutions contributed by @kfstorm; match existing brace style and do not add CSharpier
- Problem directories follow naming convention:
{NUMBER}.{Problem Name with Spaces} - Each solution file is named
Solution.{ext}(capital S) - README files use special HTML comment markers for templating (e.g.,
<!-- problem:start -->,<!-- solution:start -->,<!-- thinking:start -->) - A method heading may be followed by an optional Thinking block; see below
- Solutions should match the problem's required class/method signature from LeetCode
- New and updated solution PRs use
.github/pull_request_template.md
GitHub renders $...$ as inline math only when the opening $ is separated from the character before it. A Han character or fullwidth punctuation glued to that opening $ leaves the formula as plain text.
Follow this in every Markdown prose region that contains math, including algorithm write-ups and Thinking blocks:
- Put one ASCII space before the opening
$when the previous character is a Han character or fullwidth punctuation. - Treat an opening
$$the same way when it does not already start a line and is not already preceded by whitespace. - Keep the closing
$flush with the formula. A space before the closer drops the formula. - Do not insert this space inside fenced code, inline code, or
<pre>blocks, and do not rewrite a currency amount that uses a single$.
Use one notation for symbols in solution prose (Thinking blocks and the algorithm write-up), outside fenced code:
- Put formulas in
$...$or$$...$$. Do not put a formula in a fence or in inline code. - A single-letter symbol stays plain math:
$n$,$i$,$f[i][j]$,$O(n\log n)$. - Two lowercase letters stay plain math. That spelling is also a product (
$O(mn)$,$ab$,$dp$). - A longer code identifier is
\textit, including inside a larger formula:$\textit{nums}[i]$,$\textit{dfs}(i)$,$s_{\textit{left}}$. A digit, underscore, or capital letter makes a short token an identifier too:$\textit{s1}$,$\textit{is\_end}$,$\textit{COUNT}$. A capital letter does not glue single-letter factors into one name: write$O(C \times n)$,$O(C \times nm)$,$O(mn \times K)$, and$O(X \times Y \times n)$, not$O(\textit{Cn})$or$O(\textit{Cnm})$. Two capitals that are separate sizes inside big-O are a product as well:$O(V \times E)$,$O(C \times M)$. A numeral stays one identifier:$\textit{CM}=900$. Several uppercase words in one formula stay one italic phrase:$\textit{NOT IN}$,$\textit{UNION ALL}$,$\textit{GROUP BY}$. - A hexadecimal or binary mask is inline code, not a split formula:
`0xFFFFFFFF`,`0b1111`. Inside a larger formula, keep the whole literal in\texttt. - Words that are not identifiers stay
\text:$\text{真实值}$,$\text{Gold Medal}$,$\text{'S'}$. - Keep a TeX control word separate from the next letter:
$u\times v$, not$u\timesv$. - Operator names stay commands:
\log,\ln, and\min/\max/\gcdwhen they are called or subscripted. Do not write code identifiers with\text,\mathrm, or\operatorname. - Inline code is for a concrete code token (a call, a literal, a keyword cited as syntax), not for a symbol that also appears in a formula. Fenced blocks are for code, traces, and diagrams.
Optional in README.md / README_EN.md. When present, teach the path to the solution, not only the finished algorithm.
- Insert a
**Thinking**blockquote (use the matching Chinese label fromsolution/template.mdinREADME.md) immediately after the method heading, wrapped in<!-- thinking:start -->/<!-- thinking:end -->, before the existing algorithm write-up. Do not add a page-level or####heading — the quote box is what separates path-to-answer from the formal write-up. - Keep the original algorithm steps, complexity, and code tabs unchanged. Do not add a page-level Thinking heading.
- Walk in four beats: naive first idea and whether it fits the constraints; the bottleneck; the key observation that reshapes the problem; why this method's data structure and operation order follow from that observation.
- Write against the code that is actually in the repo. Do not describe a different algorithm. Do not restate the step-by-step that already follows.
- For later methods, only explain what the previous method still lacks (space, constants, implementation). Typical length is 3–6 sentences; hard problems may be longer. Match the existing voice (first-person plural in Chinese READMEs, LaTeX variables).
- The Chinese walkthrough must read as the same formal written prose as the algorithm section that follows. Vary sentence openings and use complete paragraphs. Do not use template slogans.
- When the prose contains inline math, keep one ASCII space between a preceding Han character or fullwidth punctuation and the opening
$. See Markdown math.