This repository contains my solutions to various LeetCode problems, organized by problem number.
| # | Title | Difficulty | Solution | LeetCode Link |
|---|---|---|---|---|
| 001 | Two Sum | Easy | Python | Problem |
| 002 | Add Two Numbers | Medium | Python | Problem |
| 003 | Longest Substring Without Repeating Characters | Medium | Python | Problem |
| 004 | Median of Two Sorted Arrays | Hard | Python | Problem |
| 005 | Longest Palindromic Substring | Medium | Python | Problem |
| 006 | Zigzag Conversion | Medium | Python | Problem |
| 007 | Reverse Integer | Medium | Python | Problem |
| 008 | String to Integer (atoi) | Medium | Python | Problem |
| 009 | Palindrome Number | Easy | Python | Problem |
- Python 3.8+ (Install from python.org or Microsoft Store)
- pip (Python package installer, included with Python)
- Visual Studio Code (recommended)
Install Python from the official website or the Microsoft Store.
Open Command Prompt and verify Python is installed:
python --version
pip --version
git clone https://github.com/yourusername/LeetCode.git
cd LeetCode
Using Command Prompt:
# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
.venv\Scripts\activate
# Confirm you're using the virtual environment Python
where python
Using PowerShell:
# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
.venv\Scripts\Activate.ps1
# If you get an execution policy error, you might need to run:
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
Using Visual Studio Code:
- Open the project folder in VS Code
- Press Ctrl+Shift+P to open the command palette
- Type "Python: Select Interpreter" and select the virtual environment
- Open a new terminal (it will automatically activate the environment)
pip install pytest
deactivate
Each solution has a corresponding test file in the test directory. To run tests:
pytest
pytest test/test_001.py
pytest -v
pytest -s
Test files follow this format:
# test/test_001.py
import pytest
import sys
import os
import importlib
# Add the parent directory to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Import the solution
solution_module = importlib.import_module("001_two_sum")
Solution = solution_module.Solution
class TestTwoSum:
def setup_method(self):
self.solution = Solution()
def test_example_1(self):
assert self.solution.twoSum([2, 7, 11, 15], 9) == [0, 1]For each problem, you can find discussions and alternative approaches on the LeetCode discussion page, linked in the table above.
- Solutions are primarily written in Python
- Each solution includes comments explaining the approach and time/space complexity
- All solutions pass LeetCode's test cases