Treat each row as a single unit. Instead of swapping individual elements, we can swap the references (in Java) or loop through columns (in languages without pointer arrays).
Since columns are not stored as contiguous variables, you must iterate through each row and swap the specific column values. Codehs 8.1.5 Manipulating 2d Arrays
Use a nested loop and conditional assignment. Treat each row as a single unit
public static int[][] rotate90(int[][] matrix) { int n = matrix.length; int[][] rotated = new int[n][n]; for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { rotated[c][n - 1 - r] = matrix[r][c]; } } return rotated; } Use a nested loop and conditional assignment
// Swap two rows by reference public static void swapRows(int[][] arr, int r1, int r2) { int[] temp = arr[r1]; arr[r1] = arr[r2]; arr[r2] = temp; }