Untitled
unknown
plain_text
2 years ago
754 B
13
Indexable
class Solution {
public void rotate(int[][] matrix) {
int numSquares = matrix.length / 2;
for (int i = 0; i < numSquares; i++) {
for (int j = i; j < matrix.length - i - 1; j++) {
int topleft = matrix[i][j];
int topright = matrix[j][matrix.length - i - 1];
int bottomright = matrix[matrix.length - i - 1][matrix.length - j - 1];
int bottomleft = matrix[matrix.length - j - 1][i];
matrix[matrix.length - i - 1][matrix.length - j - 1] = topright;
matrix[j][matrix.length - i - 1] = topleft;
matrix[matrix.length - j - 1][i] = bottomright;
matrix[i][j] = bottomleft;
}
}
}
}Editor is loading...
Leave a Comment