LeetCode Was HARD Until I Learned These 15 Patterns
When I first started solving problems on LeetCode, I felt like I was drowning in complexity. Some problems seemed unsolvable, and the sheer variety of challenges left me frustrated. However, over time, I discovered that LeetCode problems often follow certain patterns. Once I recognized these patterns, everything changed. I went from struggling through problems to solving them confidently and efficiently.
In this article, I’ll share the 15 patterns that helped me conquer LeetCode. If you’re stuck in a similar spot, understanding these patterns can significantly improve your problem-solving skills.
1. Two Pointers
The Two Pointers technique is a powerful strategy for working with arrays or strings. Typically used when the input is sorted or when you need to find pairs or subarrays.
Example: Find Two Numbers That Sum to a Target
function twoSum(nums, target) {
let left = 0, right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) return [nums[left], nums[right]];
if (sum < target) left++;
else right--;
}
return [];
}
- Explanation: By starting with two pointers at the ends of the array, we can quickly find pairs that sum to the target in a sorted array.