逆时针(反)旋转 NxN 矩阵 90 度 [英] Rotate NxN Matrix Counter(anti)-Clockwise 90 Degress

查看:60
本文介绍了逆时针(反)旋转 NxN 矩阵 90 度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维矩阵 M[N][N],我需要逆时针旋转 90 度.我看过很多顺时针旋转的答案,但我找不到逆时针.这两种操作有多相似?

I have a 2D Matrix M[N][N] that I need to rotate counter-clockwise by 90 degrees. I have seen many answers for clockwise rotation but I cannot find counter-clockwise. How similar are the two operations?

推荐答案

如果你颠倒每一行的顺序,然后从顺时针方向以相反的顺序取行,你会得到一个逆时针旋转.

If you reverse the order of each individual row and then taken rows in opposite order from a clockwise rotation, you get a count-clockwise rotation.

A B C                  G D A               A D G                  C F I
D E F -> Clockwise ->  H E B -> Reverse -> B E H  -> Opposite ->  B E H
G H I                  I F C    Rows       C F I     Ordering     A D G

Matrix                                                            Counter
                                                                  Clockwise

如果您已经有可用的顺时针旋转算法,通常以相反的顺序对原始矩阵进行顺时针旋转会更容易(并且计算效率更高).

Usually it's easier (and more computationally efficient) to do a clockwise rotation rotation on the original matrix in reverse order if you already have a clockwise rotating algorithm available.

1 2 3                9 8 7                 3 6 9
4 5 6 -> Reverse  -> 6 5 4 -> Clockwise -> 2 5 8
7 8 9    Indices     3 2 1                 1 4 7

Matrix                                     Counter
                                           Clockwise

您也可以只顺时针旋转 3 圈就可以逆时针旋转.

You can also just take 3 clockwise rotations to get to a counter clockwise rotation.

尽管在现实中,根据您的目的直接编辑顺时针算法通常相当容易.所以如果你不关心效率并且不想通过改变旋转方向的逻辑来工作,我只会使用上面的选项.

Though in reality it's usually fairly easy to edit the clockwise algorithm to your purposes directly. So I'd only use the above options if you don't care about efficiency and don't want to work through the logic of changing the direction of rotation.

这篇关于逆时针(反)旋转 NxN 矩阵 90 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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