逆时针旋转矩阵M * N的每个环 [英] Rotate each ring of the matrix M*N anticlockwise

查看:76
本文介绍了逆时针旋转矩阵M * N的每个环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法沿逆时针方向旋转 M * N 矩阵.我的代码对于 3 * 3 矩阵可以正常工作,但是当我尝试其他任何情况时都无法正常工作,请假设我正在为 4 * 4 矩阵进行操作,然后仅外部元素正在旋转,而内部4个元素(即6,7,10,11)没有旋转.我的输入是1-16个数字,作为 4 * 4 矩阵:

I am not able to rotate a M*N matrix in anticlockwise direction. My code is working properly for 3*3 matrix, but when I try for any other case it is not working assume I am doing it for 4*4 matrix then only outer elements are rotating and inner 4 elements (i.e. 6,7,10,11) are not rotating. My input is 1-16 numbers as 4*4 matrix:

{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }

static void antiRotateMatrix(int m, int n, int mat[][]) {
    int row = 0, col = n - 1;
    int prev, curr;
    while (row < m && col < n) {
        if (row + 1 == m || col - 1 == 0) {
            break;
        }
        prev = mat[row + 1][col];
        for (int i = col; i >= 0; i--) {
            curr = mat[row][i];
            mat[row][i] = prev;
            prev = curr;
        }
        row++;

        for (int i = row; i < m; i++) {
            curr = mat[i][0];
            mat[i][0] = prev;
            prev = curr;
        }
        n--;

        if (row < m) {
            for (int i = n - 2; i <= col; i++) {
                curr = mat[m - 1][i];
                mat[m - 1][i] = prev;
                prev = curr;
            }
        }
        m--;
        if (col <= n) {
            for (int i = m - 1; i >= row; i--) {
                curr = mat[i][col];
                mat[i][col] = prev;
                prev = curr;
            }
        }
        col++;
    }
    for (int i = 0; i <= 3; i++) {
        for (int j = 0; j <= 3; j++)
            System.out.print(mat[i][j] + " ");
        System.out.print("\n");
    }
}

对于 4 * 4 ,我应该得到

{{2,3,4,8},{1,7,11,12},{5,6,10,16},{9,13,14,15}}

但是我得到了

{{2,3,4,8},{1,6,7,12},{5,10,11,16},{9,13,14,15}}

推荐答案

在逆时针方向旋转每个图层 M * N 的矩阵元素:

Rotate matrix element of each layer M*N in anticlockwise direction:

for z in range(r):#r = n times to rotate your element of each layer
    top = 0
    bottom = len(matrix)-1

    left = 0
    right = len(matrix[0])-1

    while left < right and top < bottom: # anticlockwise rotation of each layer.
        prev = matrix[top+1][right]
        for i in range(right,left-1,-1):
            curr = matrix[top][i]
            matrix[top][i]= prev
            prev =curr
        top += 1
        for i in range(top,bottom+1):
            curr = matrix[i][left]
            matrix[i][left]=prev
            prev=curr
        left += 1
        for i in range(left,right+1):
            curr =matrix[bottom][i]
            matrix[bottom][i]=prev
            prev = curr
        bottom -=1
        for i in range(bottom,top-1,-1):
            curr = matrix[i][right]
            matrix[i][right]=prev
            prev = curr
        right -=1

要顺时针转到> https://www.geeksforgeeks.org/rotate-matrix-元素/

我希望您可以将此 #python 代码应用于 #java ,逻辑是相同的.或在hackerrank中选择python.

I hope you can apply this #python code to #java, logic is the same. Or select python in hackerrank.

这篇关于逆时针旋转矩阵M * N的每个环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆