原题地址:https://oj.leetcode.com/problems/rotate-image/

题意:

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

解题思路:先将矩阵转置,然后将矩阵的每一行翻转,就可以得到所要求的矩阵了。

代码:

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):
            for j in range(i+1, n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        for i in range(n):
            matrix[i].reverse()
        return matrix

 

相关文章:

  • 2021-07-21
  • 2022-02-20
  • 2021-08-24
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2021-07-04
  • 2021-12-04
  • 2021-09-25
  • 2022-02-03
  • 2022-02-20
相关资源
相似解决方案