📜 WAYPOINT LESSON
Rows, columns, and rotation
⭐⭐⭐ advanced⏳ 10 min read📍 Lesson 185 of 256
g[r][c] always; g.length vs g[0].length; the 90° rotation derived once and verified on 2x3.
Rows and columns are different axes. The exam's 2D traps are all axis confusions:
g[r][c]— row first, always.g[0]is the first ROW, not the first column.g.lengthis the row count;g[0].lengthis the column count. Rectangular grids can haveg.length != g[0].length— a rotation changes which one is which.- Row-major traversal (for r, then for c) visits in storage order; column-major (for c, then for r) is the one FRQs ask for when they say "process column by column".
The 90° clockwise rotation is the canonical axis exercise:
out[c][rows - 1 - r] = g[r][c]. Derive it once slowly: the old row
r becomes the new column (rows-1-r) counted from the right; the old
column c becomes the new row c. Verify on a 2×3 before you trust it.