LeetCode Online Judge is a website containing many algorithm questions. Most of them are real interview questions of Google, Facebook, LinkedIn, Apple, etc. This repo shows my solutions by Swift with the code style strictly follows the RayWenderlich Swift Style Guide. Please feel free to reference and STAR to support this repo, thank you!
Problem Status shows the latest progress to all 1000+ questions. Currently we have 400+ completed solutions. Note: questions with ♥ mark means that you have to Subscript to premium membership of LeetCode to unlock them.
- Array
- String
- Linked List
- Stack
- Queue
- Tree
- Dynamic programming
- Depth-first search
- Breadth-first search
- Math
- Search
- Sort
- Graph
- Design
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Reverse Linked List | Swift | Easy | O(n) | O(1) |
Palindrome Linked List | Swift | Easy | O(n) | O(1) |
Swap Nodes in Pairs | Swift | Easy | O(n) | O(1) |
Remove Linked List Elements | Swift | Easy | O(n) | O(1) |
Remove Duplicates from Sorted List | Swift | Easy | O(n) | O(1) |
Remove Duplicates from Sorted List II | Swift | Medium | O(n) | O(1) |
Remove Nth Node From End of List | Swift | Easy | O(n) | O(1) |
Odd Even Linked List | Swift | Medium | O(n) | O(1) |
Rotate List | Swift | Medium | O(n) | O(1) |
Reorder List | Swift | Medium | O(n) | O(1) |
Merge Two Sorted Lists | Swift | Easy | O(n) | O(1) |
Merge k Sorted Lists | Swift | Hard | O(mlogn) | O(1) |
Partition List | Swift | Medium | O(n) | O(1) |
Reverse Nodes in k-Group | Swift | Hard | O(n) | O(1) |
LRU Cache | Swift | Hard | O(1) | O(1) |
LFU Cache | Swift | Hard | O(1) | O(1) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Min Stack | Swift | Easy | O(1) | O(n) |
Max Stack | Swift | Easy | O(n) | O(n) |
Valid Parentheses | Swift | Easy | O(n) | O(n) |
Longest Valid Parentheses | Swift | Hard | O(n) | O(n) |
Evaluate Reverse Polish Notation | Swift | Medium | O(n) | O(n) |
Exclusive Time of Functions | Swift | Medium | O(n) | O(n) |
Simplify Path | Swift | Medium | O(n) | O(n) |
Remove K Digits | Swift | Medium | O(n) | O(n) |
Ternary Expression Parser | Swift | Medium | O(n) | O(n) |
Binary Tree Preorder Traversal | Swift | Medium | O(n) | O(n) |
Binary Tree Inorder Traversal | Swift | Medium | O(n) | O(n) |
Binary Search Tree Iterator | Swift | Medium | O(n) | O(n) |
Binary Tree Postorder Traversal | Swift | Hard | O(n) | O(n) |
Decode String | Swift | Medium | O(n) | O(n) |
Basic Calculator | Swift | Hard | O(n) | O(n) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Implement Queue using Stacks | Swift | Easy | O(n) | O(n) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Word Ladder | Swift | Medium | O(nm) | O(nm) |
Evaluate Division | Swift | Medium | O(n^2) | O(n) |
Shortest Distance from All Buildings | Swift | Hard | O((mn)^2) | O(mn) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Closest Binary Search Tree Value | Swift | Easy | O(logn) | O(1) |
Closest Binary Search Tree Value II | Swift | Hard | O(n) | O(n) |
Search in Rotated Sorted Array | Swift | Hard | O(logn) | O(1) |
Search in Rotated Sorted Array II | Swift | Medium | O(logn) | O(1) |
Find Minimum in Rotated Sorted Array | Swift | Medium | O(logn) | O(1) |
Find Minimum in Rotated Sorted Array II | Swift | Hard | O(logn) | O(1) |
Search a 2D Matrix | Swift | Medium | O(log(m + n)) | O(1) |
Search a 2D Matrix II | Swift | Medium | O(m + n) | O(1) |
Search for a Range | Swift | Medium | O(logn) | O(1) |
Search Insert Position | Swift | Medium | O(logn) | O(1) |
Peak Index in a Mountain Array | Swift | Easy | O(logn) | O(1) |
Find Peak Element | Swift | Medium | O(logn) | O(1) |
Random Pick with Weight | Swift | Medium | O(logn) | O(1) |
Sqrt(x) | Swift | Medium | O(logn) | O(1) |
Median of Two Sorted Arrays | Swift | Hard | O(log(m + n)) | O(1) |
Minimize Max Distance to Gas Station | Swift | Hard | O(nlogm) | O(1) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Merge Sorted Array | Swift | Easy | O(n) | O(1) |
Sort Colors | Swift | Medium | O(n) | O(1) |
Wiggle Sort | Swift | Medium | O(n) | O(1) |
Wiggle Sort II | Swift | Medium | O(nlogn) | O(n) |
Sort Transformed Array | Swift | Medium | O(n) | O(1) |
Top K Frequent Elements | Swift | Medium | O(nlogn) | O(n) |
Meeting Rooms | Swift | Easy | O(nlogn) | O(1) |
Meeting Rooms II | Swift | Medium | O(nlogn) | O(n) |
Merge Intervals | Swift | Hard | O(nlogn) | O(n) |
Alien Dictionary | Swift | Hard | O(nm) | O(nm) |
Kth Largest Element in an Array | Swift | Medium | O(nlogn) | O(n) |
Array Partition I | Swift | Easy | O(nlogn) | O(n) |
Insert Interval | Swift | Hard | O(n) | O(1) |
Largest Number | Swift | Medium | O(nlogn) | O(1) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Number of Connected Components in an Undirected Graph | Swift | Medium | O(nlogn) | O(n) |
Graph Valid Tree | Swift | Medium | O(nlogn) | O(n) |
Number of Islands II | Swift | Hard | O(klogmn) | O(mn) |
Course Schedule | Swift | Medium | O(n) | O(n) |
Course Schedule II | Swift | Medium | O(n) | O(n) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Shuffle an Array | Swift | Easy | O(n) | O(1) |
Design HashMap | Swift | Easy | O(n) | O(n) |
Design Tic-Tac-Toe | Swift | Medium | O(1) | O(n) |
Flatten Nested List Iterator | Swift | Medium | O(n) | O(n) |
Flatten 2D Vector | Swift | Medium | O(n) | O(n) |
Implement Trie (Prefix Tree) | Swift | Medium | O(n) | O(n) |
Add and Search Word - Data structure design | Swift | Medium | O(24^n) | O(n) |
Insert Delete GetRandom O(1) | Swift | Medium | O(1) | O(n) |
LRU Cache | Swift | Hard | O(1) | O(n) |
All O`one Data Structure | Swift | Hard | O(1) | O(n) |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Race Car | Swift | Hard | ★★★★★★ |
Plus One | Swift | Easy | ★★★★★★ |
Number of Islands | Swift | Medium | ★★★★ |
Summary Ranges | Swift | Medium | ★★★★ |
Perfect Squares | Swift | Medium | ★★★★ |
Merge Intervals | Swift | Hard | ★★★ |
Valid Parentheses | Swift | Easy | ★★★ |
Trapping Rain Water | Swift | Hard | ★★ |
Merge k Sorted Lists | Swift | Hard | ★★ |
Longest Consecutive Sequence | Swift | Hard | ★★ |
Find Peak Element | Swift | Medium | ★★ |
Power of Two | Swift | Easy | ★★ |
Spiral Matrix | Swift | Medium | ★★ |
Sliding Window Maximum | Swift | Hard | ★★ |
Pow(x, n) | Swift | Medium | ★★ |
Letter Combinations of a Phone Number | Swift | Medium | ★★ |
Heaters | Swift | Easy | ★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
3Sum | Swift | Medium | ★★★★★★ |
Valid Palindrome | Swift | Easy | ★★★★★★ |
Valid Palindrome II | Swift | Easy | ★★★★★★ |
Move Zeroes | Swift | Easy | ★★★★★★ |
Remove Invalid Parentheses | Swift | Hard | ★★★★★★ |
Add Binary | Swift | Easy | ★★★★★ |
Two Sum | Swift | Easy | ★★★★★ |
Binary Tree Paths | Swift | Easy | ★★★★ |
Letter Combinations of a Phone Number | Swift | Medium | ★★★★ |
Merge k Sorted Lists | Swift | Hard | ★★★★ |
Reverse Linked List | Swift | Easy | ★★★ |
Merge Intervals | Swift | Hard | ★★★ |
Number of Islands | Swift | Medium | ★★★ |
Reverse Linked List | Swift | Easy | ★★★ |
Expression Add Operators | Swift | Hard | ★★★ |
Subsets | Swift | Medium | ★★★ |
Sort Colors | Swift | Medium | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Game of Life | Swift | Medium | ★★★★★★ |
Meeting Rooms II | Swift | Medium | ★★★★★★ |
Valid Sudoku | Swift | Easy | ★★★★★ |
Binary Tree Vertical Order Traversal | Swift | Medium | ★★★★ |
Alien Dictionary | Swift | Hard | ★★★★ |
One Edit Distance | Swift | Medium | ★★★ |
Sudoku Solver | Swift | Hard | ★★★ |
Reverse Linked List | Swift | Easy | ★★ |
Unique Binary Search Trees | Swift | Medium | ★★ |
Minimum Window Substring | Swift | Hard | ★★ |
Remove K Digits | Swift | Medium | ★ |
Ternary Expression Parser | Swift | Medium | ★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Valid Sudoku | Swift | Easy | ★★★★ |
Spiral Matrix | Swift | Medium | ★★★★ |
Letter Combinations of a Phone Number | Swift | Medium | ★★★★ |
Group Anagrams | Swift | Medium | ★★★★ |
Word Pattern | Swift | Easy | ★★★ |
Roman to Integer | Swift | Easy | ★★★ |
Combination Sum | Swift | Medium | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Two Sum | Swift | Easy | ★★★★★ |
Text Justification | Swift | Hard | ★★★★ |
House Robber | Swift | Easy | ★★ |
Single Number | Swift | Medium | ★★ |
Word Search II | Swift | Hard | ★★ |
Add Two Numbers | Swift | Medium | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Maximum Subarray | Swift | Medium | ★★★★★★ |
Pow(x, n) | Swift | Medium | ★★★★★★ |
Merge Intervals | Swift | Hard | ★★★★★★ |
Isomorphic Strings | Swift | Easy | ★★★★★★ |
Search in Rotated Sorted Array | Swift | Hard | ★★★★★ |
Search for a Range | Swift | Medium | ★★★★★ |
Two Sum | Swift | Easy | ★★★★ |
Binary Tree Level Order Traversal | Swift | Easy | ★★★★ |
Evaluate Reverse Polish Notation | Swift | Medium | ★★★ |
Maximum Product Subarray | Swift | Medium | ★★★ |
Product of Array Except Self | Swift | Medium | ★★★ |
Symmetric Tree | Swift | Easy | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Two Sum | Swift | Easy | ★★★★★★ |
Min Cost Climbing Stairs | Swift | Easy | ★★★★ |
Number of Islands | Swift | Medium | ★★ |
Add Two Numbers | Swift | Medium | ★★ |
Reverse Linked List | Swift | Easy | ★★ |
Valid Parentheses | Swift | Easy | ★★ |
Longest Palindromic Substring | Swift | Medium | ★★ |
Trapping Rain Water | Swift | Hard | ★★ |
Longest Substring Without Repeating Characters | Swift | Medium | ★★ |
Letter Combinations of a Phone Number | Swift | Medium | ★★ |
Valid Anagram | Swift | Easy | ★★ |
Rotate Image | Swift | Medium | ★★ |
Best Time to Buy and Sell Stock | Swift | Easy | ★★ |
3Sum | Swift | Medium | ★★ |
Sliding Window Maximum | Swift | Hard | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Reverse Linked List | Swift | Easy | ★★★★★★ |
Two Sum | Swift | Easy | ★★★★★ |
String to Integer (atoi) | Swift | Easy | ★★★★ |
Add Two Numbers | Swift | Medium | ★★★★ |
Excel Sheet Column Number | Swift | Easy | ★★★★ |
Validate Binary Search Tree | Swift | Medium | ★★★ |
Merge Two Sorted Lists | Swift | Easy | ★★★ |