GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [LeetCode] 48. Rotate Image · Issue #48 · grandyang/leetcode Open Description
You are given an You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1: Example 2: Constraints:
在计算机图像处理里,旋转图片是很常见的,由于图片的本质是二维数组,所以也就变成了对数组的操作处理,翻转的本质就是某个位置上数移动到另一个位置上,比如用一个简单的例子来分析: 对于90度的翻转有很多方法,一步或多步都可以解,先来看一种直接的方法,这种方法是按顺时针的顺序去覆盖前面的数字,从四个顶角开始,然后往中间去遍历,每次覆盖的坐标都是同理,如下: (i, j) <- (n-1-j, i) <- (n-1-i, n-1-j) <- (j, n-1-i) 这其实是个循环的过程,第一个位置又覆盖了第四个位置,这里i的取值范围是 [0, n/2),j的取值范围是 [i, n-1-i),至于为什么i和j是这个取值范围,为啥i不用遍历 [n/2, n),若仔细观察这些位置之间的联系,不难发现,实际上j列的范围 [i, n-1-i) 顺时针翻转 90 度,正好就是i行的 [n/2, n) 的位置,这个方法每次循环换四个数字,如下所示: 解法一: 还有一种解法,首先以次对角线为轴翻转,然后再以x轴中线上下翻转即可得到结果,如下图所示(其中蓝色数字表示翻转轴): 解法二: 最后再来看一种方法,这种方法首先对原数组取其转置矩阵,所谓转置矩阵就是以主对角线为轴翻转,然后把每行的数字翻转可得到结果,如下所示(其中蓝色数字表示翻转轴,Github 上可能无法显示颜色,请参见博客园上的帖子): 解法三: Github 同步地址: 类似题目: Determine Whether Matrix Can Be Obtained By Rotation 参考资料: https://leetcode.com/problems/rotate-image/ https://leetcode.com/problems/rotate-image/discuss/18895/Clear-Java-solution https://leetcode.com/problems/rotate-image/discuss/18872/A-common-method-to-rotate-the-image LeetCode All in One 题目讲解汇总(持续更新中...) (欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~) 微信打赏 Venmo 打赏 Activity
MetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |