48. Rotate Image
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 15, 2024
Last updated : June 15, 2024
Related Topics : Array, Math, Matrix
Acceptance Rate : 76.62 %
void rotate(int** matrix, int matrixSize, int* matrixColSize) {
for (int row = 0; row < matrixSize; row++) {
for (int col = row + 1; col < matrixSize; col++) {
int holder = matrix[row][col];
matrix[row][col] = matrix[col][row];
matrix[col][row] = holder;
}
}
for (int row = 0; row < matrixSize; row++) {
for (int col = 0; col < matrixSize / 2; col++) {
int holder = matrix[row][col];
matrix[row][col] = matrix[row][matrixSize - col - 1];
matrix[row][matrixSize - col - 1] = holder;
}
}
}
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
for row in range(0, len(matrix)) :
for col in range(row + 1, len(matrix[0])) :
matrix[row][col], matrix[col][row] = matrix[col][row], matrix[row][col]
for row in range(0, len(matrix)) :
for col in range(0, len(matrix[0]) // 2) :
matrix[row][col], matrix[row][len(matrix[0]) - col - 1] = matrix[row][len(matrix[0]) - col - 1], matrix[row][col]