-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateImage.cs
28 lines (28 loc) · 884 Bytes
/
RotateImage.cs
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
/*
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.
*/
public class Solution {
public void Rotate(int[][] matrix)
{
int n = matrix.Length;
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n/2; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = temp;
}
}
}
}