当前位置:网站首页>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
边栏推荐
- Simulation of infrared wireless communication based on 51 single chip microcomputer
- 1-3 nodejs installation list configuration and project environment
- [WPF binding 3] listview basic binding and data template binding
- freeCodeCamp----prob_ Calculator exercise
- 常用SQL语句总结
- Understanding and small examples of unity3d object pool
- JS to find the character that appears three times in the string
- Wiper component encapsulation
- SiteServer CMS5. 0 Usage Summary
- Router object, route object, declarative navigation, programmed navigation
猜你喜欢
Deep understanding of control inversion and dependency injection
Exercise: even sum, threshold segmentation and difference (two basic questions of list object)
常用SQL语句总结
线性代数感悟之1
On lambda powertools typescript
双闭环直流调速系统matlab/simulink仿真
ASP. Net core dependency injection service life cycle
Bottom processing of stack memory in browser
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
01-初识sketch-sketch优势
随机推荐
Perception of linear algebra 2
. net cross platform principle (Part I)
Summary of common SQL statements
How to manually implement the mechanism of triggering garbage collection in node
Use of shell awk command
C# Task. Delay and thread The difference between sleep
Generating access keys using JSON webtoken
1-1 NodeJS
Promise (II)
Tdan over half
线性代数感悟之2
Further study of data visualization
[difference between Oracle and MySQL]
Further optimize Baidu map data visualization
C dapper basically uses addition, deletion, modification and query transactions, etc
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
Change Oracle to MySQL
Use of shell sed command
Compare the performance of query based on the number of paging data that meet the query conditions
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?