48.旋转图像(Rotate Image)

原创 2018年04月16日 18:53:47

题目描述

给定一个 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],
  [15,14,12,16]
], 

原地旋转输入矩阵,使其变为:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

解题思路

1.转置矩阵:swap(matrix[i][j], matrix[j][i])

1  2  3            1  4  7    
4  5  6   ---->    2  5  8
7  8  9            3  6  9

2.水平翻转矩阵:swap(matrix[i][j], matrix[i][matrix.length-1-j]

1  4  7            7  4  1
2  5  8   ---->    8  5  2
3  6  9            9  6  3
    public void rotate(int[][] matrix) {
        //转置矩阵,swap(matrix[i][j], matrix[j][i])
        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;
            }
        }
        //水平翻转矩阵,swap(matrix[i][j], matrix[i][matrix.length-1-j]
        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;
            }
        }
    }

LeetCode(48)Rotate Image

题目You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise)....
  • fly_yr
  • fly_yr
  • 2015-08-31 20:49:30
  • 2048

leetcode之图像旋转(Rotate Image)

新建一个数组,将原数组的数据按规律复制到新数组,这种方法做不到in-place,占用了额外一个数组的空间 newx = y; newy = n-1-x; 我们可以按ring by ring...
  • u011388550
  • u011388550
  • 2016-10-10 16:29:24
  • 426

[LeetCode][Java] Rotate Image

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (...
  • Evan123mg
  • Evan123mg
  • 2015-07-14 19:57:09
  • 661

【LEETCODE】48-Rotate Image [Python]

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise)....
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-01-19 17:30:17
  • 862

48 - RotateImage

Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degree...
  • hellochenlu
  • hellochenlu
  • 2016-03-04 10:12:13
  • 396

[LeetCode]48.Rotate Image

【题目】 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (cloc...
  • SunnyYoona
  • SunnyYoona
  • 2014-01-21 21:56:31
  • 2676

[leetcode-48]Rotate Image(java)

问题描述: You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockw...
  • zdavb
  • zdavb
  • 2015-08-02 21:59:36
  • 839

(Java)LeetCode-48. Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise...
  • u012848330
  • u012848330
  • 2016-09-29 15:36:17
  • 355

LeetCode Rotate Image旋转图像

其实题目更好理解为旋转一个矩阵的下标90°。实际问题实际分析,这里不用图形学的选择矩阵的计算方法了。 关键是要把下标计算好,原矩阵的数值应该对应到新的旋转矩阵的那个位置,设计好公式就很容易计算了。 ...
  • kenden23
  • kenden23
  • 2013-12-08 16:50:53
  • 10336

Leetcode_48_Rotate Image

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44216867 You are given an ...
  • pistolove
  • pistolove
  • 2015-03-12 08:48:34
  • 1277
收藏助手
不良信息举报
您举报文章:48.旋转图像(Rotate Image)
举报原因:
原因补充:

(最多只允许输入30个字)