Leetcode-Solutions

My Leetcode Solutions.

View on GitHub

48. Rotate Image

Topics: Array Math Matrix

Solution

A B C D
E F G H
I J K L
M N O P

Implementation

class Solution {
    fun rotate(matrix: Array<IntArray>): Unit {
        for (start in 0 until matrix.lastIndex) {
            val end = matrix.lastIndex - start
            for (offset in 0 until (end - start)) {
                val p = matrix[start][start + offset]
                matrix[start][start + offset] = matrix[end - offset][start]
                matrix[end - offset][start] = matrix[end][end - offset]
                matrix[end][end - offset] = matrix[start + offset][end]
                matrix[start + offset][end] = p
            }
        }
    }
}