題號:48 標題:Rotate Image 難度:Medium
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
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.
我的程式碼
void rotate(int** matrix, int matrixSize, int* matrixColSize){
int i,j,k=0;
int result[(matrixSize*matrixSize)];
for(i=0;i<matrixSize;i++){
for(j=matrixSize-1;j>=0;j--)
{ result[k]=matrix[j][i];
printf("%d",matrix[j][i]);k++;}
printf("\n");}k=0;
for(i=0;i<matrixSize;i++){
for(j=0;j<matrixSize;j++)
{ matrix[i][j]=result[k];
printf("%d",matrix[i][j]);k++;}
printf("\n");}
return 0;
}
提示
這題其實蠻簡單的,但不能再宣告一個2維陣列,此題是四個一組的換位置,不過解題時我在坐車,用手機寫的,直接偷吃步,存到一維再轉存二維,想說過不了再說,沒想到過了XD,四個一組的換法可以直接參考此題的solution
DAY22心得
我想放假