class Solution:
# @param matrix, a list of lists of integers
# @return a list of lists of integers
def rotate(self, matrix):
matrix.reverse()
for i in range(len(matrix)):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
Short Python inplace solution  50ms

In terms of interview, you should be able to use
.reverse()
in most of the complex problem. Ofc you shouldn't use this in problems likereverse a string
.It is always helpful to ask interviewer "can I use
reverse()
in this question? If not I can do the reverse by hand". If the interviewer says no, then you can easily change thematrix.reverse()
into a helper function such asreverseMatrix(matrix)
:reverseMatrix(self, matrix): n = len(matrix) for i in n / 2: matrix[i], matrix[n  1  i] = matrix[n  1  i], matrix[i] or simply: reverseMatrix(self, matrix): matrix[:] = matrix[::1]