当前位置:网站首页>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;
}
}
}
边栏推荐
- Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
- 吃透Chisel语言.05.Chisel基础(二)——组合电路与运算符
- China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
- File creation, writing, reading, deletion (transfer) in go language
- Why should Base64 encoding be used for image transmission
- The font of markdown grammar is marked in red
- xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
- R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
- 【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
- 去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
猜你喜欢

Innovation and development of independent industrial software

Vscode common plug-ins summary

C # WPF realizes the real-time screen capture function of screen capture box

Use of tiledlayout function in MATLAB

Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators

JVM memory layout detailed, illustrated, well written!

基于51单片机的超声波测距仪
![[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

Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT

MySQL version 8 installation Free Tutorial
随机推荐
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
安装Mysql
Detailed index of MySQL
How to package QT and share exe
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
Use of tiledlayout function in MATLAB
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
Why should Base64 encoding be used for image transmission
Common content type correspondence table
Incremental ternary subsequence [greedy training]
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
为什么图片传输要使用base64编码
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
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
Product identification of intelligent retail cabinet based on paddlex
markdown 语法之字体标红
【C 题集】of Ⅶ
2022 practice questions and mock exams for the main principals of hazardous chemical business units
【信息检索】链接分析
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators