48. Rotate Image

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

 Example 1:

 def rotate(matrix):

    """
    Next point, row = previous column
                col = N - 1 - previous row
    """
    N = len(matrix)
   
    for row in range(N//2 + N%2):
        for col in range(N // 2):            
            a = row, col
            b = col, N-1-row
            c = N-1-row, N-1-col
            d = N-1-col, row
            temp = matrix[d[0]][d[1]]
            matrix[d[0]][d[1]] = matrix[c[0]][c[1]]
            matrix[c[0]][c[1]] = matrix[b[0]][b[1]]
            matrix[b[0]][b[1]] = matrix[a[0]][a[1]]
            matrix[a[0]][a[1]] = temp

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break