博客
关于我
LeetCode 48 旋转图像
阅读量:188 次
发布时间:2019-02-28

本文共 1918 字,大约阅读时间需要 6 分钟。

题目

给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1

图片

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]

输出:[[7,4,1],[8,5,2],[9,6,3]]

示例 2

图片

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

示例 3

输入:matrix = [[1]]

输出:[[1]]

示例 4

输入:matrix = [[1,2],[3,4]]

输出:[[3,1],[4,2]]

提示

  • matrix.length == n
  • matrix[i].length == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

来源:力扣(LeetCode)

链接:

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

方法一:辅助数组

i行第j个元素旋转后变成倒数第i行第j个元素,于是有

matrixNew[j][col -1 - i] = matrix[i][j]

public class Solution {     // 辅助数组  public void rotate(int[][] matrix) {         if(matrix == null || matrix.length < 1 || matrix[0].length < 1) return;      int row = matrix.length, col = matrix[0].length;      int[][] matrixNew = new int[row][col];      for(int i = 0; i < row; i++){             for(int j = 0; j < col; j++){                 matrixNew[j][col - 1 - i] = matrix[i][j];          }      }      for(int i = 0; i < row; i++){             for(int j = 0; j < col; j++)              matrix[i][j] = matrixNew[i][j];      }  }}

时间复杂度:O(n^2)

空间复杂度:O(n^2)

方法二:2次翻转

首先,沿着水平方轴进行水平翻转,再沿着主对角线翻转一次即可

class Solution {       public void rotate(int[][] matrix) {           if(matrix == null || matrix.length < 1 || matrix[0].length < 1) return;        int n = matrix.length;        // 水平翻转        for(int i = 0; i < n / 2; i++){               for(int j = 0; j < n; j++){                   int tmp = matrix[i][j];                matrix[i][j] = matrix[n - 1 - i][j];                matrix[n - 1 - i][j] = tmp;            }        }        // 对角线翻转        for(int i = 0; i < n; i++){               for(int j = 0; j < i; j++){    // 这里条件一定是 j < i 不是 j < n                int tmp = matrix[i][j];                matrix[i][j] = matrix[j][i];                matrix[j][i] = tmp;             }        }    }}

时间复杂度:O(n^2)

空间复杂度:O(1)

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>