PROBLEM DESCRIPTION
You are given a N x N
2D matrix A representing an image.
Rotate the image by 90 degrees (clockwise).
You need to do this in place
. Update the given matrix A
.
SOLUTION
This problem is same as: Rotate Image
This problem can be solved by taking the transpose of the given matrix and then reversing each row one by one (reflection of the matrix).
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
public class Solution {
public void rotate(ArrayList<ArrayList<Integer>> a) {
int n = a.size();
// transpose
for(int i=0; i<n; i++){
for(int j=i; j<n; j++){
swap(a, i,j);
}
}
// reverse row wise
for(int i=0; i<n; i++){
Collections.reverse(a.get(i));
}
}
public void swap(ArrayList<ArrayList<Integer>> a, int i, int j){
int temp = a.get(i).get(j);
a.get(i).set(j, a.get(j).get(i));
a.get(j).set(i, temp);
}
}