-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
55 lines (51 loc) · 1.46 KB
/
Solution.java
File metadata and controls
55 lines (51 loc) · 1.46 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
package leetCode_48;
/**
* @author dimdark
*/
public class Solution {
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int begin = 0;
int end = matrix.length - 1;
while (begin < end) {
int[] tempArray = matrix[begin];
matrix[begin] = matrix[end];
matrix[end] = tempArray;
++begin;
--end;
}
for (int i = 0; i < matrix.length; ++i) {
for (int j = 0; j < i; ++j) {
int tempValue = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tempValue;
}
}
}
public void antirotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int tempValue;
for (int[] row : matrix) {
int begin = 0;
int end = row.length - 1;
while (begin < end) {
tempValue = row[begin];
row[begin] = row[end];
row[end] = tempValue;
++begin;
--end;
}
}
for (int i = 0; i < matrix.length; ++i) {
for (int j = 0; j < i; ++j) {
tempValue = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tempValue;
}
}
}
}