| Problem Names | Solution Writeups | Code |
|---|---|---|
| Happy Number |
Create a set to track number that has been explored to avoid stuck in a cycle. |
|
| Plus One |
Create another vector which contains carry of each position. |
|
| Pow(x-n) |
Instead of multiply x one by one, we can do x *= x in each iteration, and update count by count *= 2 or count /= 2. Only update ans if count % 2 == 1. |
|
| Rotate Image |
Create 4 pointers, left, right, top and bottom. While left < right, loop for i in range(right - left), first save top left, then shift bottom left to top left, bottom right to bottom left, top right to bottom right and then top left to top right. Need to make use of i, so everytime top, left, right and bottom need to +/- i accordingly. |
|
| Spiral Matrix |
Create 4 pointers, left, right, top and bottom. While left <= right and top <= bottom, go through first row left to right, then top++, last col top to bottom, then right--, if top <= bottom, last row right to left, (bottom++ no matter what), if left <= right, first col bottom to top, left++ no matter what. |
|
| Set Matrix zeroes |
A good way to store needed 2d positions is to create a vector of vector that store (row, col) in the format {{r1, c1}, {r2, c2}}, so to avoid 2d matrix. Once this is created, simply replace their corresponding row and col to zeroes. |
|
| Detect Squares |
The problem asks that by given a point, find if it can form a square with any 3 points existed in the class. To check if it can form a square, first check if there exists a diagonal point (x, y) such that abs(x - pt_x) == abs(y - pt_y) and they are not equal. If this is true, further check that if there exists two point p1 and p2 such that p1 = (x, pt_y) and p2 = (pt_x, y). If this is again true, the number of cycle it can form will be number of p1 * number of p2. Check for all points in the class. |