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]
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 like
reverse 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 the
matrix.reverse() into a helper function such as
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]
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.