当前位置:网站首页>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;
}
}
}
边栏推荐
- Use of arouter
- MySQL之详解索引
- MATLAB中tiledlayout函数使用
- Unittest中的TestSuite和TestRunner
- How to operate and invest games on behalf of others at sea
- R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
- Learn kernel 3: use GDB to track the kernel call chain
- Blob, text geometry or JSON column'xxx'can't have a default value query question
- Idea shortcut keys
- Assertion of unittest framework
猜你喜欢
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Unity Shader学习(三)试着绘制一个圆
Test process arrangement (2)
Learn kernel 3: use GDB to track the kernel call chain
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
递增的三元子序列[贪心训练]
vscode 常用插件汇总
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
Mask wearing detection based on yolov1
1200. Minimum absolute difference
随机推荐
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Unity Shader学习(三)试着绘制一个圆
sharding key type not supported
游戏出海,全球化运营
sql优化之explain
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
【C 题集】of Ⅶ
JVM memory layout detailed, illustrated, well written!
2022 practice questions and mock exams for the main principals of hazardous chemical business units
ARouter的使用
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
测试流程整理(3)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
go语言中的文件创建,写入,读取,删除(转)
MATLAB中tiledlayout函数使用
【信息检索】分类和聚类的实验
Matters needing attention in overseas game Investment Agency
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结