1.问题:
Leetcode48:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
2.思路:
首先将数组进行转置,然后对每一行数据对调以下
3.代码实现:
void rotate(int** matrix, int matrixRowSize, int *matrixColSizes)
{
int i,j;
//step1:求转置矩阵
for(i = 0; i < matrixRowSize; i++){
for(j = 0; j < i; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
//step2:每一行反转
for(i = 0; i < matrixRowSize; i++){
for(j = 0; j < matrixRowSize/2; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrixRowSize-1-j];
matrix[i][matrixRowSize-1-j] = temp;
}
}
}
4.运行时间图:
近期评论