LeetCode 48. 旋转图像(Rotate Image)

题目描述

给定一个 × 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]
]

解题思路

分为两步:

  1. 首先按主对角线(左上-右下)为对称轴将矩阵交换
  2. 然后以中间列为对称轴将矩阵左右交换

代码

 1 class Solution {
 2 public:
 3     void rotate(vector<vector<int>>& matrix) {
 4         int n = matrix.size();
 5         if(n == 0) return;
 6         for(int i = 0; i < n; i++)
 7             for(int j = i + 1; j < n; j++)
 8                 swap(matrix[i][j], matrix[j][i]);
 9         for(int j = 0; j < n / 2; j++)
10             for(int i = 0; i < n; i++)
11                 swap(matrix[i][j], matrix[i][n - j - 1]);
12     }
13 };

原文地址:https://www.cnblogs.com/wmx24/p/9541323.html

时间: 2024-07-31 05:10:33

LeetCode 48. 旋转图像(Rotate Image)的相关文章

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], [

leetcode || 48、Rotate Image

problem: 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? Hide Tags Array 题意:将一个矩阵顺时针旋转90度 thinking: (1)题目要求原址操作 (2)先将矩阵 转置,再将矩阵沿中间对称左右翻转 code: class Solution {

LeetCode:旋转图像【48】

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,

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

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? 题目标签:Array 这道题目给了我们一个n * n的矩阵,让我们旋转图片.题目要求in-place,所以就不能用额外的空间了.一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢.只好去看看别人的方法,看完觉得,自己

leetCode 48.Rotate Image (旋转图像) 解题思路和方法

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? 思路:其实就是旋转数组,没有什么难度,代码如下: public class Solution { public void rotate(int[][] matrix) { int[][] a =

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

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? 思路:把矩阵看成是由多个正方形一层一层包围起来的.转动的时候就是转动正方形的边,只要把边上的每个数依次替换就好,四个方向也就是进行四次替换,依次对每一层进行替换. 总层数 = matrix.length/2,第i层从第mat

LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazing!".(forgivig my poor English!) 代码如下(代码怎么写已经不重要了!): class Solution { public: void rotate(vector<vector<int>>& matrix) { int r=matrix.