LeetCode-旋转矩阵

#LeetCode

题目描述

![[Pasted image 20231103104942.png]]

初次解法

  • 递归思想
    • 递归终止条件:矩阵形态为 1x1 与 2x2
    • 递归迭代过程:矩阵形态NXN -> (N-2)X(N-2).分别是头尾截取一位。
    • 递归操作过程:将矩阵四周看作4个一维数组,遍历交换这四个数组的元素。
    • 如图:
      ![[c929c56fb5d7179af148f7a5de14fc6.jpg]]
class Solution {

    public void rotate(int[][] matrix) {

       recursion(matrix, 0, matrix[0].length-1, 0, matrix[0].length-1);

    }

  

    public void recursion(int[][] matrix, int urow,int drow, int lcol,int rcol){

        //终止条件

        if(drow - urow + 1 == 1) return ;

        else if(drow - urow + 1 == 2) {

            int temp = matrix[urow][lcol];

            matrix[urow][lcol] = matrix[drow][lcol];

            matrix[drow][lcol] = matrix[drow][rcol];

            matrix[drow][rcol] = matrix[urow][rcol];

            matrix[urow][rcol] = temp;

            return ;

        }

        //递归操作

        int i = 0;

        while(i < rcol - lcol){

            int temp = matrix[urow][lcol + i];

            matrix[urow][lcol + i] = matrix[drow - i][lcol];

            matrix[drow - i][lcol] = matrix[drow][rcol - i];

            matrix[drow][rcol - i] = matrix[urow + i][rcol];

            matrix[urow + i][rcol] = temp;

            i++;

        }

        //递归迭代

        recursion(matrix, urow + 1, drow - 1, lcol + 1, rcol - 1);

        return ;

    }

}

优化解法

  • 题目要求不使用额外的内存空间。
  • 递归比较占用栈内存,修改为迭代法看看。
  • 改了发现也没节省多少内存。
class Solution {

    public void rotate(int[][] matrix) {

        // 初始状态额外负一层,以方便后续循环运行最外层的矩阵。

        int urow = -1;

        int drow = matrix[0].length;

        int lcol = -1;

        int rcol = matrix[0].length;

        while(urow <= drow && lcol <= rcol){

            urow = urow + 1;

            drow = drow - 1;

            lcol = lcol + 1;

            rcol = rcol - 1;

            int i = 0;

            while(i < rcol - lcol){

                int temp = matrix[urow][lcol + i];

                matrix[urow][lcol + i] = matrix[drow - i][lcol];

                matrix[drow - i][lcol] = matrix[drow][rcol - i];

                matrix[drow][rcol - i] = matrix[urow + i][rcol];

                matrix[urow + i][rcol] = temp;

                i++;

            }

        }

        return;

    }

  

}

三次解法(官方)

  • 用翻转代替旋转
  • 图形变换:矩阵翻转90度=矩阵沿着水平轴翻转->矩阵再沿着主对角线翻转
class Solution {

    public void rotate(int[][] matrix) {

        // 先水平翻转 在主对角线翻转

        int n = matrix.length;

  

        //水平翻转

        for(int i = 0; i < n / 2; i++){

            for(int j = 0; j < n; j++){

                int temp = matrix[i][j];

                matrix[i][j] = matrix[n-i-1][j];

                matrix[n-i-1][j] = temp;

            }

        }

        //主对角线翻转

        for(int i = 0; i < n; i++){

            for(int j = 0; j <= i; j++){

                int temp = matrix[i][j];

                matrix[i][j] = matrix[j][i];

                matrix[j][i] = temp;

            }

        }

        return;

    }

  

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值