-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path48. Rotate Image.cpp
54 lines (50 loc) · 1.29 KB
/
48. Rotate Image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
48. Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
*/
#include<stdio.h>
#include<stdlib.h>
void rotate(int** matrix, int matrixRowSize, int matrixColSize) {
int i,j,k;
int col,s,t,u,v,w;
int row = (matrixRowSize+1)/2;
for( i = 0; i < row; i++ ){
u = i;
col = matrixColSize-1-i;
for( j = i; j < col; j++ ){
v = j;
s = matrix[matrixColSize-1-j][i];
for( k = 0; k < 4; k++ ){
t = matrix[u][v];
matrix[u][v] = s;
s = t;
w = v;
v = matrixColSize-1-u;
u = w;
}
}
}
}
int main()
{
int** matrix;
int matrixRowSize = 4;
matrix = (int**)malloc(sizeof(int*)*matrixRowSize);
for( int i = 0; i < matrixRowSize; i++ ){
*(matrix + i) = (int*)malloc(sizeof(int)*matrixRowSize);
}
matrix[0][0] =1 ;matrix[0][1] = 2;matrix[0][2] = 3;matrix[0][3] = 4;
matrix[1][0] =5 ;matrix[1][1] = 6;matrix[1][2] = 7;matrix[1][3] = 8;
matrix[2][0] =9 ;matrix[2][1] = 10;matrix[2][2] = 11;matrix[2][3] = 12;
matrix[3][0] =13 ;matrix[3][1] = 14;matrix[3][2] = 15;matrix[3][3] = 16;
rotate(matrix,matrixRowSize,matrixRowSize);
for( int i = 0; i < matrixRowSize; i++ ){
for( int j = 0; j < matrixRowSize; j++ ){
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
}