当前位置:网站首页>C语言力扣第48题之旋转图像。辅助数组
C语言力扣第48题之旋转图像。辅助数组
2022-07-31 00:16:00 【管二狗绝不摆烂】
给定一个 n × n 的二维矩阵 matrix
表示一个图像。请你将图像顺时针旋转 90 度。
你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。
示例 1:
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[[7,4,1],[8,5,2],[9,6,3]]
示例 2:
输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] 输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
提示:
n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
方法思路:
思想与矩阵转置完全相同,有兴趣可参考本人的另一篇博客
考研复习之数据结构笔记(七)数组和串(上)(包含数组的相关内容)_管二狗绝不摆烂的博客-CSDN博客
矩阵的压缩存储
我们以题目中的示例二
作为例子,分析将图像旋转 90 度之后,这些数字出现在什么位置。
对于矩阵中的第一行而言,在旋转后,它出现在倒数第一列的位置:
并且,第一行的第 x 个元素在旋转后恰好是倒数第一列的第 x 个元素。
对于矩阵中的第二行而言,在旋转后,它出现在倒数第二列的位置:
对于矩阵中的第三行和第四行同理。这样我们可以得到规律:
对于矩阵中第 i 行的第 j 个元素,在旋转后,它出现在倒数第 i 列的第 j 个位置。
我们将其翻译成代码。由于矩阵中的行列从 000 开始计数,因此对于矩阵中的元素 matrix[row][col],在旋转后,它的新位置为 matrixnew[col][n−row−1]。
这样以来,我们使用一个与 matrix大小相同的辅助数组 matrixnew{matrix},临时存储旋转后的结果。我们遍历 matrix 中的每一个元素,根据上述规则将该元素存放到 matrixnew{matrix}_ 中对应的位置。在遍历完成之后,再将 matrixnew{matrix}_中的结果复制到原数组中即可。
void rotate(int** matrix, int matrixSize, int* matrixColSize) {
int matrix_new[matrixSize][matrixSize];
for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < matrixSize; j++) {
matrix_new[i][j] = matrix[i][j];
}
}
for (int i = 0; i < matrixSize; ++i) {
for (int j = 0; j < matrixSize; ++j) {
matrix[j][matrixSize - i - 1] = matrix_new[i][j];
}
}
}
边栏推荐
- Shell script if statement
- .NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
- MySQL中substring与substr区别
- 从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
- Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
- How to solve types joiplay simulator does not support this game
- 2D转换模块&&媒体查询
- transition过渡&&animation动画
- How to import game archives in joiplay emulator
- A Brief Talk About MPI
猜你喜欢
Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。
SWM32系列教程6-Systick和PWM
WEB安全基础 - - -漏洞扫描器
How to use joiplay emulator
After writing business code for many years, I found these 11 doorways, which only experts know
作业:iptables防止nmap扫描以及binlog
MySQL面试题
状态机动态规划之股票问题总结
web漏洞之需要准备的工作
How to install joiplay emulator rtp
随机推荐
Method for deduplication of object collection
写了多年业务代码,我发现了这11个门道,只有内行才知道
MySQL数据库进阶篇
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
软件开发设计流程
firewalld
Android security optimization - APP reinforcement
【VisDrone数据集】YOLOV3训练VisDrone数据集步骤与结果
software development design process
2D转换模块&&媒体查询
How to solve types joiplay simulator does not support this game
How to ensure the consistency of database and cache data?
46.<list链表的举列>
IOT跨平台组件设计方案
How to import game archives in joiplay emulator
Learn Scope from a Compilation Perspective!
What are the efficient open source artifacts of VSCode
joiplay模拟器rtp如何安装
The first level must project independently
Jetpack Compose学习(8)——State及remeber