|
| 1 | +# Official Sol: https://tinyurl.com/rtyo3wq |
| 2 | +class Solution(object): |
| 3 | + def rotate(self, matrix): |
| 4 | + """ |
| 5 | + :type matrix: List[List[int]] |
| 6 | + :rtype: None Do not return anything, modify matrix in-place instead. |
| 7 | + """ |
| 8 | + row, col = len(matrix), len(matrix[0]) |
| 9 | + |
| 10 | + # transpose matrix |
| 11 | + for currRow in range(row): |
| 12 | + for currCol in range(currRow, col): |
| 13 | + matrix[currRow][currCol], matrix[currCol][currRow] = matrix[currCol][currRow], matrix[currRow][currCol] |
| 14 | + |
| 15 | + # reverse each row |
| 16 | + for currRow in range(row): |
| 17 | + matrix[currRow].reverse() |
| 18 | + return matrix |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +# Source: https://tinyurl.com/tr62top |
| 25 | +class Solution(object): |
| 26 | + def rotate(self, matrix): |
| 27 | + """ |
| 28 | + :type matrix: List[List[int]] |
| 29 | + :rtype: None Do not return anything, modify matrix in-place instead. |
| 30 | + """ |
| 31 | + row, col = len(matrix), len(matrix[0]) |
| 32 | + |
| 33 | + firtRow, lastRow = 0, row - 1 |
| 34 | + while firtRow < lastRow: |
| 35 | + matrix[firtRow] , matrix[lastRow] = matrix[lastRow], matrix[firtRow] |
| 36 | + firtRow += 1 |
| 37 | + lastRow -= 1 |
| 38 | + |
| 39 | + # transpose matrix |
| 40 | + for currRow in range(row): |
| 41 | + for currCol in range(currRow, col): |
| 42 | + matrix[currRow][currCol], matrix[currCol][currRow] = matrix[currCol][currRow], matrix[currRow][currCol] |
| 43 | + |
| 44 | + return matrix |
0 commit comments