본문 바로가기
<개인공부> - IT/[Python]

Rotate Image (Rotate array values 90 degrees, clockwise)

by Aggies '19 2019. 1. 16.
반응형

Example 1:

Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]


This problem is to rotate the array values by 90 degrees. There is a constraint for this question.


* Do not assign 2D array


My approach is as below

1. Transpose the array

2. Reverse the each row


class Solution:

    def rotate(self, matrix):

        """

        :type matrix: List[List[int]]

        :rtype: void Do not return anything, modify matrix in-place instead.

        """

        

        for i in range(len(matrix)):

            for j in range(i, len(matrix)):

                temp = matrix[i][j]

                matrix[i][j] = matrix[j][i]

                matrix[j][i] = temp

            matrix[i].reverse()


reference site : https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/770/discuss

반응형