当前位置:网站首页>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
边栏推荐
- Router object, route object, declarative navigation, programmed navigation
- C语言函数详解
- Devexpress GridView add select all columns
- [二叉数] 二叉树的最大深度+N叉树的最大深度
- STM32 entry development board choose wildfire or punctual atom?
- Shell - introduction, variables, and basic syntax
- Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
- 1217_使用SCons生成目标文件
- Use of todesk remote control software
- C语言程序设计之函数的构造
猜你喜欢
ASP. Net core JWT certification
Tdan over half
超分之TDAN
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
常用SQL语句总结
Matlab / Simulink simulation of double closed loop DC speed regulation system
1-4 configuration executable script of nodejs installation
为什么有些人说单片机简单,我学起来这么吃力?
Future 用法详解
随机推荐
Promise (III)
freeCodeCamp----prob_ Calculator exercise
EF core in ASP Generate core priority database based on net entity model
1-3 nodejs installation list configuration and project environment
Shell - introduction, variables, and basic syntax
El cascade and El select click elsewhere to make the drop-down box disappear
ClickHouse-表引擎
Indexes and views in MySQL
Promise (II)
JS failed to change all variables and changed to the return method. Finally, the problem was solved
1-3 components and modules
Understanding and small examples of unity3d object pool
超分之TDAN
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
Excel quickly and automatically fills the contents of a row on a blank cell
Further optimize Baidu map data visualization
How does matlab draw the curve of known formula and how does excel draw the function curve image?
Self use learning notes - connectingstring configuration
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
C语言函数详解