Better LeetCode
Refactor LeetCode solutions to follow clean code principles: clear logic, intuitive variable names, minimal comments, and idiomatic patterns.
Core Principles
- Clear over clever: Prefer readable solutions that are easy to understand at first glance
- Minimal comments: Code should be self-documenting through good naming and clear structure
- Idiomatic patterns: Use well-known algorithms and data structures with their standard implementations
- Concise variable names: Use short but meaningful names that follow common conventions
Refactoring Guidelines
Variable Naming
Use standard, memorable variable names for common patterns:
- Pointers:
slow, fast, left, right, i, j, k
- Lists/Arrays:
nums, arr, res, ans
- Trees:
root, node, left, right
- Graphs:
graph, node, visited, adj
- Strings:
s, t, word, pattern
- Counters:
count, cnt, freq
- Results:
res, ans, result
Code Structure
Do:
- Keep functions focused and simple
- Use early returns to reduce nesting
- Prefer while loops for unclear iteration counts
- Use for loops with range for known iterations
- Initialize variables close to usage
Avoid:
- Excessive comments explaining obvious logic
- Overly complex one-liners that sacrifice readability
- Unnecessary temporary variables
- Deep nesting (>3 levels)
Common Patterns
Two Pointers:
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
Sliding Window:
left = 0
for right in range(len(s)):
# expand window
while condition:
# shrink window
left += 1
Binary Search:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
DFS (Tree):
def dfs(node):
if not node:
return
# process node
dfs(node.left)
dfs(node.right)
BFS:
from collections import deque
queue = deque([start])
while queue:
node = queue.popleft()
# process node
if condition:
queue.append(next_node)
Optimization Process
When refactoring code:
- Analyze: Identify the algorithm/pattern being used
- Simplify: Remove unnecessary complexity and redundancy
- Rename: Apply standard variable naming conventions
- Structure: Organize code for maximum readability
- Verify: Ensure logic correctness is maintained
Focus on readability and maintainability over micro-optimizations unless performance is explicitly required.
1---2name: better-leetcode3description: Optimize and refactor LeetCode solutions to be more elegant, readable, and idiomatic. Use when the user provides code from a LeetCode problem and wants to improve code quality, simplify logic, enhance variable naming, or make the solution more concise and maintainable. Also use when the user asks to "optimize", "refactor", "improve", or "clean up" LeetCode code.4---56# Better LeetCode78Refactor LeetCode solutions to follow clean code principles: clear logic, intuitive variable names, minimal comments, and idiomatic patterns.910## Core Principles11121. **Clear over clever**: Prefer readable solutions that are easy to understand at first glance132. **Minimal comments**: Code should be self-documenting through good naming and clear structure143. **Idiomatic patterns**: Use well-known algorithms and data structures with their standard implementations154. **Concise variable names**: Use short but meaningful names that follow common conventions1617## Refactoring Guidelines1819### Variable Naming2021Use standard, memorable variable names for common patterns:2223- **Pointers**: `slow`, `fast`, `left`, `right`, `i`, `j`, `k`24- **Lists/Arrays**: `nums`, `arr`, `res`, `ans`25- **Trees**: `root`, `node`, `left`, `right`26- **Graphs**: `graph`, `node`, `visited`, `adj`27- **Strings**: `s`, `t`, `word`, `pattern`28- **Counters**: `count`, `cnt`, `freq`29- **Results**: `res`, `ans`, `result`3031### Code Structure3233**Do:**34- Keep functions focused and simple35- Use early returns to reduce nesting36- Prefer while loops for unclear iteration counts37- Use for loops with range for known iterations38- Initialize variables close to usage3940**Avoid:**41- Excessive comments explaining obvious logic42- Overly complex one-liners that sacrifice readability43- Unnecessary temporary variables44- Deep nesting (>3 levels)4546### Common Patterns4748**Two Pointers:**49```python50slow = fast = head51while fast and fast.next:52 fast = fast.next.next53 slow = slow.next54```5556**Sliding Window:**57```python58left = 059for right in range(len(s)):60 # expand window61 while condition:62 # shrink window63 left += 164```6566**Binary Search:**67```python68left, right = 0, len(nums) - 169while left <= right:70 mid = (left + right) // 271 if nums[mid] == target:72 return mid73 elif nums[mid] < target:74 left = mid + 175 else:76 right = mid - 177```7879**DFS (Tree):**80```python81def dfs(node):82 if not node:83 return84 # process node85 dfs(node.left)86 dfs(node.right)87```8889**BFS:**90```python91from collections import deque9293queue = deque([start])94while queue:95 node = queue.popleft()96 # process node97 if condition:98 queue.append(next_node)99```100101## Optimization Process102103When refactoring code:1041051. **Analyze**: Identify the algorithm/pattern being used1062. **Simplify**: Remove unnecessary complexity and redundancy1073. **Rename**: Apply standard variable naming conventions1084. **Structure**: Organize code for maximum readability1095. **Verify**: Ensure logic correctness is maintained110111Focus on readability and maintainability over micro-optimizations unless performance is explicitly required.