返回 Skill 列表
extension
分类: 开发与工程无需 API Key

better-leetcode

优化并重构LeetCode解决方案,使其更加优雅、易读和符合惯用法。当用户提供了一个LeetCode问题的代码,并希望提高代码质量、简化逻辑、增强变量命名,或者使解决方案更加简洁和易于维护时使用。此外,在用户要求“优化”、“重构”、“改进”或“清理”LeetCode代码时也应使用此规则。

person作者: jakexiaohubgithub

Better LeetCode

Refactor LeetCode solutions to follow clean code principles: clear logic, intuitive variable names, minimal comments, and idiomatic patterns.

Core Principles

  1. Clear over clever: Prefer readable solutions that are easy to understand at first glance
  2. Minimal comments: Code should be self-documenting through good naming and clear structure
  3. Idiomatic patterns: Use well-known algorithms and data structures with their standard implementations
  4. 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:

  1. Analyze: Identify the algorithm/pattern being used
  2. Simplify: Remove unnecessary complexity and redundancy
  3. Rename: Apply standard variable naming conventions
  4. Structure: Organize code for maximum readability
  5. Verify: Ensure logic correctness is maintained

Focus on readability and maintainability over micro-optimizations unless performance is explicitly required.