当前位置:网站首页>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;
}
}
}
边栏推荐
- Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
- 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
- 递增的三元子序列[贪心训练]
- MySQL 5 installation and modification free
- GCC【6】- 编译的4个阶段
- 如何游戏出海代运营、游戏出海代投
- C# wpf 实现截屏框实时截屏功能
- 吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
- Innovation and development of independent industrial software
- sharding key type not supported
猜你喜欢

Mask wearing detection based on yolov1

Install MySQL

吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器

Excel快速合并多行数据

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!

Unity Shader学习(三)试着绘制一个圆

docker-compose公网部署redis哨兵模式
![[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions](/img/7a/9b559313b407f9a12cbaed7bebd4dc.png)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions

Learn kernel 3: use GDB to track the kernel call chain

Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
随机推荐
92.(cesium篇)cesium楼栋分层
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
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
LifeCycle
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
Detailed index of MySQL
Why should Base64 encoding be used for image transmission
Learning projects are self-made, and growth opportunities are self created
Use of tiledlayout function in MATLAB
Excel quickly merges multiple rows of data
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
Gorm read / write separation (rotation)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
海外游戏代投需要注意的
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)