当前位置:网站首页>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;
}
}
}
边栏推荐
- Assertion of unittest framework
- GCC [6] - 4 stages of compilation
- Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
- 按照功能对Boost库进行分类
- Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
- WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
- R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
- 数据仓库面试问题准备
- 10.(地图数据篇)离线地形数据处理(供Cesium使用)
- 海外游戏代投需要注意的
猜你喜欢
Vscode common plug-ins summary
MySQL之详解索引
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
Test process arrangement (2)
软件测试之测试评估
【信息检索】链接分析
C# wpf 实现截屏框实时截屏功能
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
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
随机推荐
数据仓库面试问题准备
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
MATLAB中tiledlayout函数使用
mac redis安装与使用,连接远程服务器 redis
qt 怎么检测鼠标在不在某个控件上
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
LiveData
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
2022 practice questions and mock exams for the main principals of hazardous chemical business units
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
php 日志调试
递增的三元子序列[贪心训练]
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
卷积神经网络经典论文集合(深度学习分类篇)
【信息检索】分类和聚类的实验
为什么图片传输要使用base64编码
Understand chisel language thoroughly 07. Chisel Foundation (IV) - bundle and VEC