当前位置:网站首页>LeetCode中等题之旋转图像
LeetCode中等题之旋转图像
2022-08-04 09:15: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
来源:力扣(LeetCode)
解题思路
这种矩阵变换类的题目,如果是方阵难度将会大大降低,针对题目中所给的限制条件,可以考虑用镜像操作代替旋转,这也是常用的等效操,所以可以先上下翻转然后再按主对角线反转。
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
""" Do not return anything, modify matrix in-place instead. """
n=len(matrix)
for i in range(n//2):
matrix[i],matrix[n-i-1]=matrix[n-i-1],matrix[i]
for i in range(n):
for j in range(i+1):
matrix[i][j],matrix[j][i]=matrix[j][i],matrix[i][j]

边栏推荐
猜你喜欢

DOM简述

双指针方法

Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...

张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生

cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
![Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]](/img/56/2ad889a3a740d0a7624b25f6307616.png)
Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]

今日睡眠质量记录71分

我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此

王爽汇编语言第四章:第一个程序

Fiddler(二)-手机抓包502错误解决方法
随机推荐
有坦荡的远方
leetcode单调栈经典例题——最大矩形
GBsae 8c 数据库使用中报错,肿么办?
cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
记录十条工作中便利的API小技巧
spark算子讲解
The separation configuration Libpq is supported, speaking, reading and writing
No module named 'flask_misaka' has been resolved [BUG solution]
leetcode每天5题-Day06
It is found that several WRH tables are locked, what should I do?
v-model原理,在“radio”、“checkbox”、“select”、修饰符
下午14:00面试,14:08低着头出来了 ,问的实在是太...
MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
Libpq 是否支持读写分离配置
.NET深入解析LINQ框架(五:IQueryable、IQueryProvider接口详解)
How to import data from PG to kingbaseES
【云驻共创】HCSD 大咖直播–就业指南
leetcode经典例题——56.合并区间
VRRP + MSTP configuration, huawei eNSP experiment 】 【
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败