【嘎】数组-面试题 01.07. 旋转矩阵-看题解

题目:

给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 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]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-matrix-lcci

解答:

class Solution {
    // 正方形
    public void rotate(int[][] matrix) {
        int[][] res = new int[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            int temp[] = new int[matrix.length];
            for (int j = matrix.length - 1; j >= 0 ; j--) {
                temp[matrix.length - j - 1] = matrix[j][i];
            }
            res[i] = temp;
            
        }
        for (int i = 0; i < res.length; i++) {
            matrix[i] = res[i];
        }
        // matrix = res; 这个没有用
    }
}

 关于数组初始化总是会忘记,要在有提示的编辑器里才能准确写出。。。

题解中还有其他解决方法,,脑阔疼不想看。。以后再看

越努力越幸运~ 加油ヾ(◍°∇°◍)ノ゙
原文地址:https://www.cnblogs.com/utomboy/p/12651549.html