LeetCode:旋转图像【48】

LeetCode:旋转图像【48】

题目描述

给定一个 × n 的二维矩阵表示一个图像。

将图像顺时针旋转 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]
]

题目分析

  最笨的方法是模拟,可是在短时间内根本无法准确提取出模拟的所有边界判断条件,代码异常冗余,且无法进行优化。

  然后怎么办呢?我们分析题目可以看出一个隐含条件,就是所给的矩阵一定是正方形的。因为我们不能新建数组,那么原有数组为长方形的话,无论如何我们都是无法表示结果的。

  到这里然后呢?我们首先将其进行行列转置

  

  接着,我们再关于中心轴做镜像变换,swap(arr[i][j], arr[i][matrix.length-1-j])

  

  最后我们应该思考一下为什么这样可以呢?

Java题解

    public void rotate(int[][] matrix) {
        for(int i = 0;i<matrix.length;i++) {
            for (int j = i; j < matrix[0].length; j++) {
                int temp = 0;
                temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        for(int i =0 ; i<matrix.length; i++){
            for(int j = 0; j<matrix.length/2; j++){
                int temp = 0;
                temp = matrix[i][j];
                matrix[i][j] = matrix[i][matrix.length-1-j];
                matrix[i][matrix.length-1-j] = temp;
            }
        }
    }

  

原文地址:https://www.cnblogs.com/MrSaver/p/9897148.html

时间: 2024-08-30 00:23:07

LeetCode:旋转图像【48】的相关文章

LeetCode(48):旋转图像

Medium! 题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 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], [1

Leetcode题目48.旋转图像(中等)

题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 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,

【一天一道LeetCode】#48. Rotate Image

一天一道LeetCode系列 (一)题目 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? (二)解题 90度旋转图像,我们不难看出 matrix[i][j]=tmp[j][n?i?1]注:tmp=matrix 经过这样的变换后,图像就旋转了90度. class Solu

&lt;LeetCode OJ&gt; 48. Rotate Image

Total Accepted: 69879 Total Submissions: 199786 Difficulty: Medium 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? Subscribe to see which companies asked this

LeetCode第[48]题(Java):Rotate Image

题目:矩阵旋转 难度:Medium 题目内容: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate anot

LeetCode(48)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? 分析 本地使得二维矩阵,旋转90角度. 通过实际数据分析,通过两个步骤的元素交换可实现目标: 按照主对角线,将对称元素交换 按照列,将对称列元素全部交换 即可达到,使得二维矩阵,本地旋转90个角度. AC代码 cla

笔试题61. LeetCode OJ (48)

这个题的意思是给你一个2D的图像(我们知道2D图像是由(x,y)像素点组成的),题目要求我们将这幅图形顺时针旋转90度,确实是一个比较有趣的题目. 如果你是第一次拿到这个题的话确实比较难动手,但是一旦你见过这类似的题目(比如:剑指offer上面的螺旋数组),那么你就瞬间有思路了.我先画个图解释一下这个题的思路: 主要思路也是由外向里,每次旋转掉一层,重复上面操作,知道循环到最里面,这时候就完成了整体的旋转.代码: class Solution { public: void rotate(vect

Python 解leetcode:48. Rotate Image

题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place inst

LeetCode——48. 旋转图像

给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 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], [