-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage.kt
More file actions
33 lines (29 loc) · 865 Bytes
/
RotateImage.kt
File metadata and controls
33 lines (29 loc) · 865 Bytes
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
package leetcode
/**
* Problem description on [LeetCode](https://leetcode.com/problems/rotate-image/)
*/
class RotateImage {
fun rotate(matrix: Array<IntArray>) {
swapDiagonal(matrix)
swapVertical(matrix)
}
private fun swapVertical(matrix: Array<IntArray>) {
for (i in matrix.indices) {
for (j in 0 until matrix.size / 2) {
val k = matrix.size - j - 1
val temp = matrix[i][j]
matrix[i][j] = matrix[i][k]
matrix[i][k] = temp
}
}
}
private fun swapDiagonal(matrix: Array<IntArray>) {
for (i in matrix.indices) {
for (j in i + 1 until matrix.size) {
val temp = matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = temp
}
}
}
}