-
Notifications
You must be signed in to change notification settings - Fork 3
/
_01_07_MatrixRotation.groovy
42 lines (36 loc) · 1.27 KB
/
_01_07_MatrixRotation.groovy
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
package Ch01_ArraysAndStrings
/** Given a square matrix, rotate it 90° */
class _01_07_MatrixRotation {
static rotate(int[][] matrix) {
def copy = rotateCopy(matrix, matrix.length)
def original = rotateInPlace(matrix, matrix.length)
assert copy == original
original
}
/** Complexity: O(width*height), uses same amount of extra space */
static rotateCopy(int[][] matrix, int size) {
def result = new int[size][size]
matrix.eachWithIndex { int[] rowElements, int row ->
rowElements.eachWithIndex { int entry, int column ->
result[column][size - row - 1] = entry
}
}
result
}
/** Complexity: O(width*height), doesn't use extra space */
static rotateInPlace(int[][] matrix, int size) {
for (row in 0..<(size / 2))
for (column in row..<(size - row - 1))
rotate(matrix, column, row, size - 1)
matrix
}
static rotate(int[][] matrix, int column, int row, int size) {
def previous = matrix[row][column]
4.times {
(row, column) = [column, size - row]
def current = matrix[row][column]
matrix[row][column] = previous
previous = current
}
}
}