Study guide for LeetCode using Swift. Includes data structure implementations, pattern explanations, and classic solved problems.
- Read the data structure in
DataStructures/to understand the implementation - Read the pattern in
Patterns/to understand the solving technique - Solve the problem before looking at the solution in
Problems/ - Compare your solution with the guide and analyze complexity
| Phase | Topic | Data Structure | Problems | Status |
|---|---|---|---|---|
| 1 | Arrays & Hashing | Array, Dictionary, Set |
Two Sum, Contains Duplicate, Group Anagrams, Top K Frequent | |
| 2 | Two Pointers | Array |
Valid Palindrome, Two Sum II, 3Sum, Container With Most Water | |
| 3 | Sliding Window | Array, Dictionary |
Best Time to Buy/Sell Stock, Longest Substring Without Repeating | |
| 4 | Stack | Stack |
Valid Parentheses, Min Stack, Daily Temperatures, Evaluate RPN | |
| 5 | Binary Search | Array |
Binary Search, Search Rotated Array, Find Min Rotated | |
| 6 | Linked List | LinkedList |
Reverse Linked List, Merge Two Lists, Linked List Cycle, Reorder List | |
| 7 | Trees | BinaryTree |
Invert Tree, Max Depth, Same Tree, Level Order, Validate BST | |
| 8 | Heap / Priority Queue | Heap |
Kth Largest Element, Last Stone Weight, K Closest Points | |
| 9 | Graphs | Graph |
Number of Islands, Clone Graph, Course Schedule, Pacific Atlantic | |
| 10 | Dynamic Programming | -- | Climbing Stairs, Coin Change, Longest Common Subsequence, House Robber | |
| 11 | Backtracking | -- | Subsets, Permutations, Combination Sum, N-Queens | |
| 12 | Greedy | -- | Jump Game, Merge Intervals, Gas Station |
| Notation | Name | Example |
|---|---|---|
| O(1) | Constant | Array access by index |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Simple loop |
| O(n log n) | Linearithmic | Optimal sorting (merge sort) |
| O(n^2) | Quadratic | Nested double loop |
| O(2^n) | Exponential | Subsets, backtracking |
| O(n!) | Factorial | Permutations |
// MARK: - Problem
// Link: https://leetcode.com/problems/...
// Difficulty: Easy/Medium/Hard
// MARK: - Approach
// Explanation of the strategy
// MARK: - Complexity
// Time: O(...)
// Space: O(...)
// MARK: - Solution
func solution(...) -> ... { }
// MARK: - Tests
assert(solution(...) == expected)Each .swift file can be run directly:
swift Problems/01-ArraysAndHashing/TwoSum.swift./run_tests.sh