Share my simple python code (in place, space comlexity O(1))


  • 0
    A
    class Solution:
        # @param matrix, a list of lists of integers
        # @return nothing (void), do not return anything, modify matrix in-place instead.
        def rotate(self, matrix):
            if matrix is None:
                return
    
            m = len(matrix)
    
            for i in range(m):
                for j in range(i,m-i-1):
                    row = j
                    col = m-1-i
                    while not (row == i and col == j):
                        mid = matrix[row][col]
                        matrix[row][col] = matrix[i][j]
                        matrix[i][j] = mid
                        row1 = row
                        col1 = col
                        row = col1
                        col = m-1-row1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.