48. Rotate Image

题目

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).

思路

对二维数组先按照对称轴反转,再左右反转

代码

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

效率

48. Rotate Image_第1张图片
image.png

你可能感兴趣的:(48. Rotate Image)