Series: leetcode
python
282 lines
· Updated 2026-02-03
solution_library.py
leetcode/solution_library.py
#!/usr/bin/env python3
"""
Solution Library for LeetCode Problems
Contains actual implementations of solutions for common LeetCode problems.
"""
from typing import Dict, Optional, List
class SolutionLibrary:
"""Library of actual solution implementations for LeetCode problems."""
def __init__(self):
self.solutions = self._initialize_solutions()
def _initialize_solutions(self) -> Dict:
"""Initialize the solution library with actual implementations."""
return {
# Problem 1: Two Sum
'two-sum': {
'python': '''def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
Use hash map to store seen numbers and their indices.
Time: O(n), Space: O(n)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []''',
'java': '''public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{};
}''',
'cpp': '''vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (map.find(complement) != map.end()) {
return {map[complement], i};
}
map[nums[i]] = i;
}
return {};
}''',
'c': '''int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
*returnSize = 2;
int* result = (int*)malloc(2 * sizeof(int));
for (int i = 0; i < numsSize; i++) {
for (int j = i + 1; j < numsSize; j++) {
if (nums[i] + nums[j] == target) {
result[0] = i;
result[1] = j;
return result;
}
}
}
*returnSize = 0;
return NULL;
}''',
'csharp': '''public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> map = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++) {
int complement = target - nums[i];
if (map.ContainsKey(complement)) {
return new int[] { map[complement], i };
}
map[nums[i]] = i;
}
return new int[] {};
}''',
'javascript': '''var twoSum = function(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
};''',
'go': '''func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := m[complement]; ok {
return []int{idx, i}
}
m[num] = i
}
return []int{}
}''',
},
# Problem 2: Add Two Numbers
'add-two-numbers': {
'python': '''def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
"""
Add two numbers represented as linked lists.
Time: O(max(m, n)), Space: O(max(m, n))
"""
dummy = ListNode(0)
current = dummy
carry = 0
while l1 or l2 or carry:
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
total = val1 + val2 + carry
carry = total // 10
current.next = ListNode(total % 10)
current = current.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next''',
'java': '''public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode current = dummy;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int val1 = l1 != null ? l1.val : 0;
int val2 = l2 != null ? l2.val : 0;
int sum = val1 + val2 + carry;
carry = sum / 10;
current.next = new ListNode(sum % 10);
current = current.next;
l1 = l1 != null ? l1.next : null;
l2 = l2 != null ? l2.next : null;
}
return dummy.next;
}''',
'cpp': '''ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;
int carry = 0;
while (l1 || l2 || carry) {
int val1 = l1 ? l1->val : 0;
int val2 = l2 ? l2->val : 0;
int sum = val1 + val2 + carry;
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
l1 = l1 ? l1->next : nullptr;
l2 = l2 ? l2->next : nullptr;
}
return dummy->next;
}''',
},
# Problem 11: Container With Most Water
'container-with-most-water': {
'python': '''def maxArea(self, height: List[int]) -> int:
"""
Two pointers approach: start from both ends, move pointer with smaller height.
Time: O(n), Space: O(1)
"""
left, right = 0, len(height) - 1
max_area = 0
while left < right:
width = right - left
area = width * min(height[left], height[right])
max_area = max(max_area, area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area''',
'java': '''public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int maxArea = 0;
while (left < right) {
int width = right - left;
int area = width * Math.min(height[left], height[right]);
maxArea = Math.max(maxArea, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}''',
'cpp': '''int maxArea(vector<int>& height) {
int left = 0, right = height.size() - 1;
int maxArea = 0;
while (left < right) {
int width = right - left;
int area = width * min(height[left], height[right]);
maxArea = max(maxArea, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}''',
'csharp': '''public int MaxArea(int[] height) {
int left = 0, right = height.Length - 1;
int maxArea = 0;
while (left < right) {
int width = right - left;
int area = width * Math.Min(height[left], height[right]);
maxArea = Math.Max(maxArea, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}''',
},
}
def get_solution(self, title_slug: str, language: str) -> Optional[str]:
"""Get solution for a problem by title slug and language."""
lang_lower = language.lower()
# Normalize language names
lang_map = {
'python': 'python',
'python3': 'python',
'java': 'java',
'cpp': 'cpp',
'c++': 'cpp',
'c': 'c',
'csharp': 'csharp',
'c#': 'csharp',
'javascript': 'javascript',
'typescript': 'javascript', # Similar syntax
'go': 'go',
}
normalized_lang = lang_map.get(lang_lower, lang_lower)
if title_slug in self.solutions:
return self.solutions[title_slug].get(normalized_lang)
return None
def has_solution(self, title_slug: str) -> bool:
"""Check if solution exists for a problem."""
return title_slug in self.solutions
Related articles
leetcode
python
Updated 2026-02-03
implement_missing_solutions.py
implement_missing_solutions.py — python source code from the leetcode learning materials (leetcode/implement_missing_solutions.py).
Read article →
leetcode
python
Updated 2026-02-03
leetcode_downloader.py
leetcode_downloader.py — python source code from the leetcode learning materials (leetcode/leetcode_downloader.py).
Read article →
leetcode
python
Updated 2026-02-03
solution_generator.py
solution_generator.py — python source code from the leetcode learning materials (leetcode/solution_generator.py).
Read article →