当前位置:网站首页>48. 旋转图像
48. 旋转图像
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
给定一个 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]]
二、解题
翻转代替旋转
class Solution {
public void rotate(int[][] matrix) {
//这题是找规律
//先将数组按照水平翻转 ,然后在按照主对角线翻转
int n = matrix.length;
if(matrix == null){
return ;
}
//先按照水平线翻转
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;
}
}
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hequnwang10/article/details/124333369
边栏推荐
- ASP. Net core configuration options (Part 2)
- Promise (II)
- SiteServer CMS5. 0 Usage Summary
- [related to zhengheyuan cutting tools]
- ASP. Net core dependency injection service life cycle
- C# Task. Delay and thread The difference between sleep
- Matlab / Simulink simulation of double closed loop DC speed regulation system
- Qt 修改UI没有生效
- How does matlab draw the curve of known formula and how does excel draw the function curve image?
- 练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
猜你喜欢
C# Task. Delay and thread The difference between sleep
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
01-初识sketch-sketch优势
为什么有些人说单片机简单,我学起来这么吃力?
ASP. Net core JWT certification
Matlab / Simulink simulation of double closed loop DC speed regulation system
ASP. Net core dependency injection service life cycle
uni-app黑马优购项目学习记录(下)
Deep understanding of control inversion and dependency injection
常用SQL语句总结
随机推荐
索引:手把手教你索引从零基础到精通使用
Node template engine (EJS, art template)
ASP. Net core reads the configuration file in the class library project
Router object, route object, declarative navigation, programmed navigation
Self use learning notes - connected and non connected access to database
Baidu Map Case - Zoom component, map scale component
Baidu Map 3D rotation and tilt angle adjustment
ASP. Net core configuration options (Part 1)
XTask与Kotlin Coroutine的使用对比
uni-app黑马优购项目学习记录(下)
Manually implement call, apply and bind functions
ClickHouse-SQL 操作
JS to find the character that appears three times in the string
Manually implement simple promise and its basic functions
Bottom processing of stack memory in browser
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Your brain expands and shrinks over time — these charts show how
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
Solution of Navicat connecting Oracle library is not loaded
HCIP第五次实验