Menu

Language Coverage and Conventions

Relevant source files

Purpose and Scope

This document catalogs the 10+ programming languages supported in the LeetCode solutions repository and documents the language-specific idioms, naming conventions, and coding patterns used consistently across all problem implementations. Each problem typically provides solutions in multiple languages, allowing learners to compare algorithmic implementations across different programming paradigms.

For information about automated code formatting that enforces these conventions, see Code Formatting System.


Supported Languages Overview

The repository provides solutions in 16 programming languages, ordered by prevalence and community adoption:

LanguageFile ExtensionPrimary Use CaseMemory Management
Python3.pyRapid prototyping, readabilityAutomatic (GC)
Java.javaEnterprise, verbose clarityAutomatic (GC)
C++.cppPerformance-critical, STLManual/RAII
Go.goConcurrency, simplicityAutomatic (GC)
TypeScript.tsType-safe JavaScriptAutomatic (GC)
Rust.rsMemory safety, systemsOwnership system
JavaScript.jsWeb, dynamic typingAutomatic (GC)
C#.cs.NET ecosystemAutomatic (GC)
PHP.phpWeb backendAutomatic (GC)
Scala.scalaFunctional/OOP hybridAutomatic (GC)
Swift.swiftiOS/macOS developmentARC
Ruby.rbScripting, RailsAutomatic (GC)
Kotlin.ktAndroid, JVMAutomatic (GC)
Nim.nimSystems, Python-like syntaxManual/GC hybrid
Cangjie.cjEmerging languageAutomatic (GC)
C.cLow-level, embeddedManual

Sources: solution/0000-0099/0001.Two Sum/README.md81-403 solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md77-328


File Organization and Naming

Standard File Structure

Each problem directory follows a consistent naming pattern:

solution/XXXX-XXXX/<problem_id>/
├── README.md              # Chinese documentation
├── README_EN.md           # English documentation
├── Solution.py            # Python implementation
├── Solution.java          # Java implementation
├── Solution.cpp           # C++ implementation
├── Solution.go            # Go implementation
├── Solution.ts            # TypeScript implementation
├── Solution.rs            # Rust implementation
├── Solution.js            # JavaScript implementation
├── Solution.cs            # C# implementation
├── Solution.php           # PHP implementation
├── Solution.scala         # Scala implementation
├── Solution.swift         # Swift implementation
├── Solution.rb            # Ruby implementation
├── Solution.kt            # Kotlin implementation
├── Solution.nim           # Nim implementation
├── Solution.cj            # Cangjie implementation
└── Solution.c             # C implementation

Tab Organization in Documentation

README files present solutions in collapsible tabs, ordered by language popularity:

This structure appears in both Chinese (README.md79-405) and English (README_EN.md76-402) documentation.

Sources: solution/0000-0099/0001.Two Sum/README.md1-410 solution/0000-0099/0001.Two Sum/README_EN.md1-407


Language-Specific Idioms and Patterns

Hash Table Implementation Patterns

Different languages use distinct data structures and APIs for hash-based lookups:

Example Comparison: Two Sum Hash Lookup

LanguageHash Table TypeInsertionLookup Pattern
Pythondictd[x] = i(y := target - x) in d (walrus operator)
JavaHashMap<Integer, Integer>d.put(x, i)d.containsKey(y) then d.get(y)
C++unordered_map<int, int>d[x] = id.contains(y) (C++20)
Gomap[int]intd[x] = ij, ok := d[y]; if ok
TypeScriptMap<number, number>d.set(x, i)d.has(y) then d.get(y)!
RustHashMap<i32, i32>d.insert(x, i)if let Some(&j) = d.get(&y)

Sources: solution/0000-0099/0001.Two Sum/Solution.py1-8 solution/0000-0099/0001.Two Sum/Solution.java1-14 solution/0000-0099/0001.Two Sum/Solution.cpp1-15 solution/0000-0099/0001.Two Sum/Solution.go1-12 solution/0000-0099/0001.Two Sum/Solution.ts1-12 solution/0000-0099/0001.Two Sum/Solution.rs1-16


Null/Optional Handling

Handling absence of values varies dramatically across languages:

Concrete Examples from Add Two Numbers:

LanguageNull Check PatternValue Access
Pythonl1.val if l1 else 0Conditional expression
Javal1 == null ? 0 : l1.valTernary operator
C++l1 ? l1->val : 0Pointer truthiness
Goif l1 != nil { s += l1.Val }Explicit if statement
TypeScriptl1?.val || 0Optional chaining with nullish coalescing
Rustif let Some(node) = l1 { sum += node.val; }Pattern matching
JavaScriptl1?.val || 0Optional chaining
Swiftif let node = l1 { s += node.val }Optional binding
Rubyl1.nil? ? 0 : l1.valnil? predicate
PHP$l1 !== null ? $l1->val : 0Strict comparison

Sources: solution/0000-0099/0002.Add Two Numbers/Solution.py10-18 solution/0000-0099/0002.Add Two Numbers/Solution.java17-18 solution/0000-0099/0002.Add Two Numbers/Solution.cpp18 solution/0000-0099/0002.Add Two Numbers/Solution.go14-19 solution/0000-0099/0002.Add Two Numbers/Solution.rs25-33


Loop and Iteration Patterns

Sliding Window Loop Example (from Longest Substring):

LanguageLoop StructureCounter Management
Pythonfor r, c in enumerate(s):cnt = Counter() with cnt[c] += 1
Javafor (int l = 0, r = 0; r < n; ++r)int[] cnt = new int[128] with ++cnt[c]
C++for (int l = 0, r = 0; r < n; ++r)int cnt[128]{} zero-initialized
Gofor r, c := range scnt := [128]int{} array
TypeScriptfor (let l = 0, r = 0; r < n; ++r)const cnt = new Map<string, number>()
Rustfor (r, &c) in chars.iter().enumerate()let mut cnt = [0; 128] array
PHPfor ($r = 0; $r < $n; ++$r)$cnt = array_fill(0, 128, 0)

Sources: solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.py5-6 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.java5-6 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.cpp6 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.go4


Memory Management and Ownership

Linked List Node Allocation Examples:

LanguageNode CreationMemory ModelCleanup
PythonListNode(val)Reference counting + GCAutomatic
Javanew ListNode(s % 10)Heap allocation, GCAutomatic
C++new ListNode(s % 10)Heap allocationManual (or RAII)
Go&ListNode{s % 10, nil}Escape analysis determines heap/stackAutomatic
RustBox::new(ListNode::new(sum % 10))Heap allocation, ownedAutomatic (Drop trait)
Cmalloc(sizeof(struct ListNode))Explicit heapExplicit free()
SwiftListNode.init(s % 10)ARCAutomatic

Sources: solution/0000-0099/0002.Add Two Numbers/Solution.py15 solution/0000-0099/0002.Add Two Numbers/Solution.java19 solution/0000-0099/0002.Add Two Numbers/Solution.cpp20 solution/0000-0099/0002.Add Two Numbers/Solution.go20 solution/0000-0099/0002.Add Two Numbers/Solution.rs34 solution/0000-0099/0002.Add Two Numbers/Solution.c30-31


Data Structure Conventions

Character/String Counting Arrays

Fixed-size arrays for ASCII/Unicode character counting:

LanguageDeclarationIndexingNotes
Pythoncnt = Counter() or cnt = {}cnt[c]Uses dict for sparse data
Javaint[] cnt = new int[128];cnt[c]Direct char-to-int conversion
C++int cnt[128]{};cnt[s[r]]Zero-initialization with {}
Gocnt := [128]int{}cnt[c]Array literal
TypeScriptconst cnt = new Map<string, number>()cnt.get(s[r])Map for Unicode safety
Rustlet mut cnt = [0; 128];cnt[c as usize]Explicit type cast
Cint freq[256] = {0};freq[(unsigned char) c]Unsigned to avoid negatives
PHP$cnt = array_fill(0, 128, 0);$cnt[ord($s[$r])]ord() for ASCII value

Sources: solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.py3 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.java3 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.cpp4 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.go2


Dummy Node Pattern for Linked Lists

All languages use a dummy/sentinel node to simplify list construction:

Initialization Patterns:

  • Python: dummy = ListNode() - default constructor
  • Java: ListNode dummy = new ListNode(0); - explicit value
  • C++: ListNode dummy; (stack) or ListNode* dummy = new ListNode(0); (heap)
  • Go: dummy := &ListNode{} - struct literal with address-of
  • Rust: let mut dummy = Some(Box::new(ListNode::new(0))); - wrapped in Option
  • C: struct ListNode* dummy = malloc(sizeof(struct ListNode)); - manual allocation
  • Swift: var dummy = ListNode.init() - explicit init
  • PHP: $dummy = new ListNode(0); - object instantiation

Sources: solution/0000-0099/0002.Add Two Numbers/Solution.py10 solution/0000-0099/0002.Add Two Numbers/Solution.java13 solution/0000-0099/0002.Add Two Numbers/Solution.cpp14 solution/0000-0099/0002.Add Two Numbers/Solution.go9 solution/0000-0099/0002.Add Two Numbers/Solution.rs22 solution/0000-0099/0002.Add Two Numbers/Solution.c11-13


Language-Specific Syntax Highlights

Modern Language Features

Python: Walrus Operator and Type Hints

Source: solution/0000-0099/0001.Two Sum/Solution.py2-7

TypeScript: Non-Null Assertions and Generics

Source: solution/0000-0099/0001.Two Sum/Solution.ts1-11

Rust: Pattern Matching and Ownership

Source: solution/0000-0099/0001.Two Sum/Solution.rs1-16

C++: Contains Method (C++20)

Source: solution/0000-0099/0001.Two Sum/Solution.cpp8-9

Go: Multiple Return Values

Source: solution/0000-0099/0001.Two Sum/Solution.go6-7

Swift: Optional Binding

Source: solution/0000-0099/0001.Two Sum/Solution.swift5-7

JavaScript: Optional Chaining

Source: solution/0000-0099/0002.Add Two Numbers/Solution.js18

PHP: Isset for Array Keys

Source: solution/0000-0099/0001.Two Sum/Solution.php11-13

Ruby: Predicate Methods

Source: solution/0000-0099/0001.Two Sum/Solution.rb7-9

Nim: Functional Style

Source: solution/0000-0099/0002.Add Two Numbers/Solution.nim27-28


Cross-Language Algorithm Equivalence

Example: Two Sum Complete Implementation

The following diagram shows how the same hash table algorithm maps to code symbols across languages:

Sources: solution/0000-0099/0001.Two Sum/Solution.py solution/0000-0099/0001.Two Sum/Solution.java solution/0000-0099/0001.Two Sum/Solution.cpp solution/0000-0099/0001.Two Sum/Solution.go solution/0000-0099/0001.Two Sum/Solution.rs


Summary: Language Selection Guide

Choose...When you need...Trade-offs
PythonRapid prototyping, readable codeSlower runtime, dynamic typing
JavaEnterprise compatibility, strong typingVerbose syntax, boilerplate
C++Maximum performance, STL algorithmsComplex syntax, manual memory
GoSimplicity, concurrencyLimited generics (pre-1.18)
TypeScriptType-safe JavaScript, IDE supportCompilation step required
RustMemory safety + performanceSteep learning curve, ownership
JavaScriptWeb environments, dynamicRuntime type errors, implicit coercion
C#.NET ecosystem, Windows appsPlatform dependency
PHPWeb backends, legacy systemsInconsistent APIs
SwiftApple platforms, modern syntaxPlatform-specific
RubyScripting, metaprogrammingSlower execution
NimPython-like + compiled performanceSmaller ecosystem

Sources: All solution files in solution/0000-0099/0001.Two Sum/ solution/0000-0099/0002.Add Two Numbers/ solution/0000-0099/0003.Longest Substring Without Repeating Characters/