LeetCode · Math & Bits
Math & Bit Manipulation Problem Set
XOR tricks, bit counting, matrix rotation, and integer arithmetic. These problems test mathematical insight more than data structure knowledge.
Concept page: Interview Patterns (Bit Manipulation section)
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | XOR | LC 136 · Single Number | XOR all elements. Duplicates cancel out. O(n). |
| Easy | bit-count | LC 191 · Number of 1 Bits | n & (n-1) clears lowest set bit. Count iterations. O(32). |
| Easy | reverse | LC 190 · Reverse Bits | Extract LSB, shift into result. 32 iterations. O(1). |
| Easy | math | LC 268 · Missing Number | XOR [0..n] with array, or use sum formula. O(n). |
| Easy | DP+bits | LC 338 · Counting Bits | dp[i] = dp[i >> 1] + (i & 1). O(n). |
| Medium | math | LC 7 · Reverse Integer | Pop digits with mod/div, check overflow before push. O(log n). |
| Medium | matrix | LC 48 · Rotate Image | Transpose + reverse each row. In-place. O(n²). |
| Medium | matrix | LC 73 · Set Matrix Zeroes | Use first row/col as markers. O(1) extra space. O(m·n). |
| Medium | bit-ops | LC 371 · Sum of Two Integers | XOR for sum without carry, AND + shift for carry. Repeat until no carry. O(32). |