当前位置:网站首页>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;
}
}
}
边栏推荐
- The font of markdown grammar is marked in red
- 去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
- R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
- LiveData
- 软件测试之测试评估
- 基于YOLOv1的口罩佩戴检测
- R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
- 92.(cesium篇)cesium楼栋分层
- Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
- 去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
猜你喜欢
![Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]

【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法

392. Judgement subsequence

Unittest框架中引入TestFixture

Why should Base64 encoding be used for image transmission

基于YOLOv1的口罩佩戴检测

吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest

Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million

Mask wearing detection based on yolov1
![Incremental ternary subsequence [greedy training]](/img/92/7efd1883c21c0e804ffccfb2231602.png)
Incremental ternary subsequence [greedy training]
随机推荐
学内核之三:使用GDB跟踪内核调用链
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
2022游戏出海实用发行策略
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
ViewModel 初体验
Gorm data insertion (transfer)
Product identification of intelligent retail cabinet based on paddlex
sql优化之查询优化器
92.(cesium篇)cesium楼栋分层
Unittest中的TestSuite和TestRunner
吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
Basic mode of service mesh
Can mortgage with housing exclude compulsory execution
如何游戏出海代运营、游戏出海代投
LifeCycle
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
Xcode 异常图片导致ipa包增大问题
go语言中的文件创建,写入,读取,删除(转)