Is my code an in-place solution?


  • 0
    E

    Is my code an in-place solution? (Python)

    class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of lists of integers
    def rotate(self, matrix):
        n = len(matrix)
        for i in range((n + 1) / 2):
            for j in range(n / 2):
                cur_i, cur_j = i, j
                value = matrix[cur_i][cur_j]
                for k in range(4):
                    temp = matrix[cur_j][n - cur_i - 1]
                    matrix[cur_j][n - cur_i - 1] = value
                    value = temp
                    cur_i, cur_j = cur_j, n - cur_i - 1
        return matrix

  • 0
    B

    Yeah as long as the extra space you use is constant regardless of the input size then its in place.However your running time could be reduced to O(n2),what you have right now is O(n3)


  • 0
    D

    I think it is O(n^2). The inner most loop has range(4), which is constant


Log in to reply
 

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