This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a LeetCode solutions repository containing Python solutions to various LeetCode problems. Each problem has its own directory.
Each solution follows the naming convention: {problem_number}_{problem_name}/main.py
Examples:
1_two_sum/main.py100_same_tree/main.py155_min_stack/main.py
Each solution is a standalone Python file that can be run directly:
python {problem_number}_{problem_name}/main.pyExample:
python 1_two_sum/main.pyEach main.py file typically contains:
- Data structure definitions (when needed):
TreeNode,ListNode, or custom classes likeMinStack - Solution class: Contains the main algorithm method matching LeetCode's expected signature
- Helper functions:
list_to_tree(),list_to_linked(),linked_to_list()for test case conversion (when needed) run_test()function: Helper that executes a single test case with error handlingmain()function: Contains asampleslist of test cases and iterates through themif __name__ == "__main__": main()guard
Recent solutions follow this pattern (see 78_subsets/main.py or 100_same_tree/main.py):
class Solution:
def methodName(self, param: list[int]) -> list[int]:
# implementation
pass
def run_test(input_params, expected):
try:
result = Solution().methodName(input_params)
ok = result == expected
print(ok)
if not ok:
print(f" input: {input_params}")
print(f" expected: {expected}")
print(f" got: {result}")
except Exception as e:
print(f"ERROR -> {e}")
def main() -> None:
samples = [
# Problem examples
(input1, expected1),
(input2, expected2),
# Additional tests / edge cases
(edge_case1, expected_edge1),
]
for input_val, expected in samples:
run_test(input_val, expected)
if __name__ == "__main__":
main()Use modern Python type hints (Python 3.9+):
- Use
list[int]instead ofList[int](no import needed) - Use
dict[str, int]instead ofDict[str, int](no import needed) - Use
Type | Noneinstead ofOptional[Type](union syntax) - Only import from
typingwhen needed for special types (e.g.,from collections import deque)
Running a solution prints True for each passing test case and False (with detailed failure info) for failures. Exceptions are caught and printed with ERROR -> prefix.