-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnePointSix.java
More file actions
79 lines (63 loc) · 2.33 KB
/
OnePointSix.java
File metadata and controls
79 lines (63 loc) · 2.33 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
67
68
69
70
71
72
73
74
75
76
77
78
79
// Nicole Kulakowski
// Question 1.6
//
// 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 OnePointSix{
int dim = 0;
String matrix = "";
String[][] rotate = new String[dim][dim];
public static void main(String[] args){
// 4x4 matrix
String input = "****\n****\n****\n***0";
OnePointSix OPS = new OnePointSix(input);
//String[][] rotate = new String[dim][dim];
OPS.solution(OPS.rotate, OPS.dim);
System.out.println("Before:\n\n"+input);
System.out.println("After:\n\n"+OPS.rotate);
}
public OnePointSix(String input){
this.matrix = input;
for(int i = 0; i<matrix.length(); i++){
if(matrix.charAt(i) == '\n'){
this.dim = i;
i = matrix.length();
}
}
rotate = createMatrix();
}
public String[][] createMatrix(){
int row = 0;
int col = 0;
String[][] rotated = new String[dim][dim];
for (int i = 0; i < matrix.length(); i++){
if(matrix.charAt(i) == '\n'){
row++;
col = 0;
}
rotated[col][row] = matrix.charAt(i);
col++;
}
return rotated;
}
public void solution(String[][] matrix, int n){
for(int layer = 0; layer < n/2; ++layer){
int first = layer;
int last = n-1-layer;
for(int i = first; i<last;++i){
int offset = i-first;
//save top
String top = matrix[first][i];
//left -> top
matrix[first][i] = matrix[last-offset][first];
// bottom -> left
matrix[last-offset][first] = matrix[last][last-offset];
//right -> bottom
matrix[last][last-offset] = matrix[i][last];
//top -> right
matrix[i][last] = top;
}
}
}
}