-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageMatrix.java
40 lines (34 loc) · 1 KB
/
ImageMatrix.java
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
import java.util.Arrays;
/*
Rotate Matrix: Given an image represented by an NxN matrix, where each pixel in the image is 4
bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
*/
public class ImageMatrix{
public static void main( String [] args){
float[][] a = {{1,2,3},{4,5,6},{7,8,9}};
float[][] res = getImageMatrix(a);
for (float[] r: res)
System.out.println(Arrays.toString(r));
}
public static float[][] getImageMatrix(float[][] a){
int len = a.length;
int arrLen = a.length;
float[][] result = new float[arrLen][arrLen];
for(int i=0 ; i < arrLen ; i++){
for(int j=0 ; j < arrLen ;j++){
result[i][j] = a[len-1][j];
}
len--;
}
return swap(result,arrLen);
}
public static float[][] swap(float[][] result, int arrLen){
float[][] r = new float[arrLen][arrLen];
for(int i = 0;i < arrLen;i++){
for(int j=0 ; j < arrLen ;j++){
r[i][j] = result[j][i];
}
}
return r;
}
}