当前位置:网站首页>Leetcode T48:旋转图像
Leetcode T48:旋转图像
2022-07-04 12:51: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
思路
旋转90°等于先转置,再镜像对称。
代码
public void rotate(int[][] matrix) {
int n = matrix.length;
// 转置
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
int t = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = t;
}
}
// 镜像对称
for(int i = 0; i < n; i++) {
for(int j = 0; j < n/2; j++) {
int t = matrix[i][j]; matrix[i][j] = matrix[i][n-1-j]; matrix[i][n-1-j] = t;
}
}
}
边栏推荐
- 吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
- IP 实验室月复盘 · 第 5 期
- Use of tiledlayout function in MATLAB
- vscode 常用插件汇总
- 为什么图片传输要使用base64编码
- 10.(地图数据篇)离线地形数据处理(供Cesium使用)
- MATLAB中tiledlayout函数使用
- Excel快速合并多行数据
- 吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
猜你喜欢
测试流程整理(3)
Why should Base64 encoding be used for image transmission
软件测试之测试评估
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
基于YOLOv1的口罩佩戴检测
Qt如何实现打包,实现EXE分享
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
随机推荐
Test process arrangement (3)
LiveData
Unittest中的TestSuite和TestRunner
sharding key type not supported
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
Excel快速合并多行数据
Whether the loyalty agreement has legal effect
Data warehouse interview question preparation
IP 实验室月复盘 · 第 5 期
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
File creation, writing, reading, deletion (transfer) in go language
递增的三元子序列[贪心训练]
Mongodb commonly used 28 query statements (forward)
Common content type correspondence table
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
QT how to detect whether the mouse is on a control
Gorm data insertion (transfer)
How to operate and invest games on behalf of others at sea
Mask wearing detection based on yolov1