48. Rotate Image

The idea of this solution is first to flip the matrix, so it is upside down, and then transpose the matrix. If you don’t understand, look at the following images:

We flip the matrix by fliping the top and bottom len(matrix) / 2 rows, like in this image:

And transposing the matrix is basically making matrix[i][j] = matrix[j][i], and the flips should only flip from yellow to the blue, or blue to yellow in the following image:

So, we only need to loop from the yellow part and flip from the yellow to the blue.

The Code:

func rotate(matrix [][]int) {
	for i := 0; i < len(matrix)/2; i++ {
		newI := abs(i-len(matrix)) - 1
		for j := 0; j < len(matrix); j++ {
			matrix[newI][j], matrix[i][j] = matrix[i][j], matrix[newI][j]
		}
	}
	for i := 0; i < len(matrix); i++ {
		for j := i + 1; j < len(matrix); j++ {
			matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
		}
	}
}

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}