-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotationMatrix.cs
More file actions
66 lines (58 loc) · 1.74 KB
/
Copy pathRotationMatrix.cs
File metadata and controls
66 lines (58 loc) · 1.74 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCode.Matrix
{
[TestClass]
public class RotationMatrix
{
/// <summary>
/// First step: Perform the transpose of the matrix (Transpose is to convert rows to columns and columns to row).
/// Second step: Reverse each row of the matrix.
/// </summary>
public void Rotate(int[,] matrix)
{
// Transpose
this.Transpose(matrix);
// Reverse rows.
this.Reverse(matrix);
}
private void Transpose(int[,] matrix)
{
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for(int i=0; i< row; i++)
{
for (int j=i; j< col; j++)
{
int temp = matrix[i, j];
matrix[i,j] = matrix[j,i];
matrix[j,i] = temp;
}
}
}
private void Reverse(int[,] matrix)
{
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for (int i=0; i< row; i++)
{
for (int j=0, k = col -1; j< k; j++, k--)
{
int temp = matrix[i,k];
matrix[i,k] = matrix[i, j];
matrix[i,j] = temp;
}
}
}
[TestMethod]
public void Test90DegreeRotation()
{
int[,] matrix = new int[,] { { 5, 1, 9, 11 }, { 2, 4, 8, 10 }, { 13, 3, 6, 7 } , { 15, 14, 12, 16 } };
this.Rotate(matrix);
}
}
}