반응형
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
반응형
'<개인공부> - IT > [Python]' 카테고리의 다른 글
What is the point of float("-inf"), float("inf") (0) | 2019.01.22 |
---|---|
Regular expression practice (0) | 2019.01.17 |
python count built-in function example (0) | 2019.01.15 |
Best time to buy and sell stock II (1-line python code) (0) | 2019.01.15 |
13. Roman to Integer (0) | 2019.01.12 |