面试题 01.07. 旋转矩阵

2022/1/14 23:04:53

本文主要是介绍面试题 01.07. 旋转矩阵,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。

不占用额外内存空间能否做到?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-matrix-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public void rotate(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;

        int row1 = 0, col1 = 0, row2 = m - 1, col2 = n - 1;

        while (row1 < row2 && col1 < col2) {
            for (int i = col1; i < col2; ++i) {
                int t = matrix[row1][i];
                matrix[row1][i] = matrix[row2 - (i - col1)][col1];
                matrix[row2 - (i - col1)][col1] = matrix[row2][col2 - (i - col1)];
                matrix[row2][col2 - (i - col1)] = matrix[row1 + (i - col1)][col2];
                matrix[row1 + (i - col1)][col2] = t;
            }
            row1++;
            col1++;
            row2--;
            col2--;
        }
        
    }
}


这篇关于面试题 01.07. 旋转矩阵的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程