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.
The repository provides solutions in 16 programming languages, ordered by prevalence and community adoption:
| Language | File Extension | Primary Use Case | Memory Management |
|---|---|---|---|
| Python3 | .py | Rapid prototyping, readability | Automatic (GC) |
| Java | .java | Enterprise, verbose clarity | Automatic (GC) |
| C++ | .cpp | Performance-critical, STL | Manual/RAII |
| Go | .go | Concurrency, simplicity | Automatic (GC) |
| TypeScript | .ts | Type-safe JavaScript | Automatic (GC) |
| Rust | .rs | Memory safety, systems | Ownership system |
| JavaScript | .js | Web, dynamic typing | Automatic (GC) |
| C# | .cs | .NET ecosystem | Automatic (GC) |
| PHP | .php | Web backend | Automatic (GC) |
| Scala | .scala | Functional/OOP hybrid | Automatic (GC) |
| Swift | .swift | iOS/macOS development | ARC |
| Ruby | .rb | Scripting, Rails | Automatic (GC) |
| Kotlin | .kt | Android, JVM | Automatic (GC) |
| Nim | .nim | Systems, Python-like syntax | Manual/GC hybrid |
| Cangjie | .cj | Emerging language | Automatic (GC) |
| C | .c | Low-level, embedded | Manual |
Sources: solution/0000-0099/0001.Two Sum/README.md81-403 solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md77-328
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
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
Different languages use distinct data structures and APIs for hash-based lookups:
Example Comparison: Two Sum Hash Lookup
| Language | Hash Table Type | Insertion | Lookup Pattern |
|---|---|---|---|
| Python | dict | d[x] = i | (y := target - x) in d (walrus operator) |
| Java | HashMap<Integer, Integer> | d.put(x, i) | d.containsKey(y) then d.get(y) |
| C++ | unordered_map<int, int> | d[x] = i | d.contains(y) (C++20) |
| Go | map[int]int | d[x] = i | j, ok := d[y]; if ok |
| TypeScript | Map<number, number> | d.set(x, i) | d.has(y) then d.get(y)! |
| Rust | HashMap<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
Handling absence of values varies dramatically across languages:
Concrete Examples from Add Two Numbers:
| Language | Null Check Pattern | Value Access |
|---|---|---|
| Python | l1.val if l1 else 0 | Conditional expression |
| Java | l1 == null ? 0 : l1.val | Ternary operator |
| C++ | l1 ? l1->val : 0 | Pointer truthiness |
| Go | if l1 != nil { s += l1.Val } | Explicit if statement |
| TypeScript | l1?.val || 0 | Optional chaining with nullish coalescing |
| Rust | if let Some(node) = l1 { sum += node.val; } | Pattern matching |
| JavaScript | l1?.val || 0 | Optional chaining |
| Swift | if let node = l1 { s += node.val } | Optional binding |
| Ruby | l1.nil? ? 0 : l1.val | nil? predicate |
| PHP | $l1 !== null ? $l1->val : 0 | Strict 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
Sliding Window Loop Example (from Longest Substring):
| Language | Loop Structure | Counter Management |
|---|---|---|
| Python | for r, c in enumerate(s): | cnt = Counter() with cnt[c] += 1 |
| Java | for (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 |
| Go | for r, c := range s | cnt := [128]int{} array |
| TypeScript | for (let l = 0, r = 0; r < n; ++r) | const cnt = new Map<string, number>() |
| Rust | for (r, &c) in chars.iter().enumerate() | let mut cnt = [0; 128] array |
| PHP | for ($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
Linked List Node Allocation Examples:
| Language | Node Creation | Memory Model | Cleanup |
|---|---|---|---|
| Python | ListNode(val) | Reference counting + GC | Automatic |
| Java | new ListNode(s % 10) | Heap allocation, GC | Automatic |
| C++ | new ListNode(s % 10) | Heap allocation | Manual (or RAII) |
| Go | &ListNode{s % 10, nil} | Escape analysis determines heap/stack | Automatic |
| Rust | Box::new(ListNode::new(sum % 10)) | Heap allocation, owned | Automatic (Drop trait) |
| C | malloc(sizeof(struct ListNode)) | Explicit heap | Explicit free() |
| Swift | ListNode.init(s % 10) | ARC | Automatic |
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
Fixed-size arrays for ASCII/Unicode character counting:
| Language | Declaration | Indexing | Notes |
|---|---|---|---|
| Python | cnt = Counter() or cnt = {} | cnt[c] | Uses dict for sparse data |
| Java | int[] cnt = new int[128]; | cnt[c] | Direct char-to-int conversion |
| C++ | int cnt[128]{}; | cnt[s[r]] | Zero-initialization with {} |
| Go | cnt := [128]int{} | cnt[c] | Array literal |
| TypeScript | const cnt = new Map<string, number>() | cnt.get(s[r]) | Map for Unicode safety |
| Rust | let mut cnt = [0; 128]; | cnt[c as usize] | Explicit type cast |
| C | int 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
All languages use a dummy/sentinel node to simplify list construction:
Initialization Patterns:
dummy = ListNode() - default constructorListNode dummy = new ListNode(0); - explicit valueListNode dummy; (stack) or ListNode* dummy = new ListNode(0); (heap)dummy := &ListNode{} - struct literal with address-oflet mut dummy = Some(Box::new(ListNode::new(0))); - wrapped in Optionstruct ListNode* dummy = malloc(sizeof(struct ListNode)); - manual allocationvar dummy = ListNode.init() - explicit init$dummy = new ListNode(0); - object instantiationSources: 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
Source: solution/0000-0099/0001.Two Sum/Solution.py2-7
Source: solution/0000-0099/0001.Two Sum/Solution.ts1-11
Source: solution/0000-0099/0001.Two Sum/Solution.rs1-16
Source: solution/0000-0099/0001.Two Sum/Solution.cpp8-9
Source: solution/0000-0099/0001.Two Sum/Solution.go6-7
Source: solution/0000-0099/0001.Two Sum/Solution.swift5-7
Source: solution/0000-0099/0002.Add Two Numbers/Solution.js18
Source: solution/0000-0099/0001.Two Sum/Solution.php11-13
Source: solution/0000-0099/0001.Two Sum/Solution.rb7-9
Source: solution/0000-0099/0002.Add Two Numbers/Solution.nim27-28
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
| Choose... | When you need... | Trade-offs |
|---|---|---|
| Python | Rapid prototyping, readable code | Slower runtime, dynamic typing |
| Java | Enterprise compatibility, strong typing | Verbose syntax, boilerplate |
| C++ | Maximum performance, STL algorithms | Complex syntax, manual memory |
| Go | Simplicity, concurrency | Limited generics (pre-1.18) |
| TypeScript | Type-safe JavaScript, IDE support | Compilation step required |
| Rust | Memory safety + performance | Steep learning curve, ownership |
| JavaScript | Web environments, dynamic | Runtime type errors, implicit coercion |
| C# | .NET ecosystem, Windows apps | Platform dependency |
| PHP | Web backends, legacy systems | Inconsistent APIs |
| Swift | Apple platforms, modern syntax | Platform-specific |
| Ruby | Scripting, metaprogramming | Slower execution |
| Nim | Python-like + compiled performance | Smaller 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/
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.