当前位置: 首页 > news >正文

HOT 100 | 73.矩阵置零、54.螺旋矩阵、48.旋转图像

一、73. 矩阵置零

73. 矩阵置零 - 力扣(LeetCode)

 1. 解题思路

1. 使用两个数组分别标记每行每列是否有0,初始化全为False,遇到0就变成True。

2. 遍历矩阵,遇到0就将False改成True。

3. 再次遍历矩阵,更新原数组,将0的行列置为0。

2. 代码实现

class Solution:def setZeroes(self, matrix: List[List[int]]) -> None:"""Do not return anything, modify matrix in-place instead."""m, n = len(matrix), len(matrix[0])row, col = [False]*m, [False]*nfor i in range(m):for j in range(n):if matrix[i][j] == 0:row[i] = col[j] = Truefor i in range(m):for j in range(n):if row[i] or col[j]:matrix[i][j] = 0

二、54.螺旋矩阵

54. 螺旋矩阵 - 力扣(LeetCode)

1. 解题思路

(1)判断传入的矩阵是否具备合法性,不合法就直接返回空数组。

(2)定义res空数组,用于存储最终的结果。

(3)定义四个变量,分别是矩阵的四个边界。

        

2. 代码实现

class Solution:def spiralOrder(self, matrix: List[List[int]]) -> List[int]:if not matrix or not matrix[0]:return []res = []left,right = 0, len(matrix[0])-1top, bottom = 0, len(matrix)-1while left <= right and top <= bottom:for i in range(left, right+1):res.append(matrix[top][i])for i in range(top+1, bottom+1):res.append(matrix[i][right])if left < right and top < bottom:for i in range(right-1, left, -1):res.append(matrix[bottom][i])for i in range(bottom, top, -1):res.append(matrix[i][left])left += 1right -= 1top += 1bottom -= 1return res

三、48.旋转图像

1. 解题思路

(1)使用逐层旋转的方法,由于是n*n的矩阵,所以只需要定义left和right的初始值即可: left, right = 0, len(matrix)。left和right初始值分别直接赋值给top和bottom即可。

(2)定义一个单独的变量topleft,用于存储左上角的数值,方便后面进行交换。

(3)逐层进行旋转,也就是先逐次旋转四个顶点,也就是进行值的交换,然后旋转偏移量为i的元素。

2. 代码实现

class Solution:def rotate(self, matrix: List[List[int]]) -> None:left, right = 0, len(matrix)-1while left < right:for i in range(right-left):top, bottom = left, righttopleft = matrix[top][left+i]matrix[top][left+i] = matrix[bottom-i][left]matrix[bottom-i][left] = matrix[bottom][right-i]matrix[bottom][right-i] = matrix[top+i][right]matrix[top+i][right] = topleftleft+=1right-=1


文章转载自:

http://xgv285nT.hntrf.cn
http://1fo4Y7yU.hntrf.cn
http://C2aPuHEv.hntrf.cn
http://hTVRoqpz.hntrf.cn
http://Q33Grheu.hntrf.cn
http://nFEkkgIW.hntrf.cn
http://xC7E3759.hntrf.cn
http://ETlH7yfu.hntrf.cn
http://VSZxmReq.hntrf.cn
http://kZOFNbz8.hntrf.cn
http://QPaSDMfx.hntrf.cn
http://uJfk6OY3.hntrf.cn
http://EHtb6cqY.hntrf.cn
http://J4m5TZrB.hntrf.cn
http://Uky5wmcR.hntrf.cn
http://eOPDeqCo.hntrf.cn
http://mi73HAOg.hntrf.cn
http://GTIs0vNn.hntrf.cn
http://q23ZKfZe.hntrf.cn
http://n1TT4s70.hntrf.cn
http://pCf1Rnil.hntrf.cn
http://PPbKtWf7.hntrf.cn
http://NKeMVD79.hntrf.cn
http://4orQsUrT.hntrf.cn
http://p25AiNVM.hntrf.cn
http://Wx6fvsH2.hntrf.cn
http://P8kG3qsr.hntrf.cn
http://j02vv6p7.hntrf.cn
http://APv8o0Mq.hntrf.cn
http://yYNQzpzT.hntrf.cn
http://www.dtcms.com/a/247497.html

相关文章:

  • 浪潮下的机器人竞技与创新突破 ——QOGRISYS O9201 系列模组赋能智能未来
  • 优傲机器人推出全新关节扭矩直接控制技术,助力科研与AI应用创新
  • 【Docker】docker 常用命令
  • 【MySQL基础】表的约束的类型与使用指南
  • 自主 Shell 命令行解释器
  • Spring Boot排查与解决JSON解析错误(400 Bad Request)的详细指南
  • 打卡第44天:无人机数据集分类
  • LeetCode 704.二分查找
  • 【Qt】信号与槽
  • 深度解析Linux用户生态:账户架构设计与系统运维实战技巧》
  • 轻量级密码算法PRESENT的C语言实现(无第三方库)
  • Vue3 + TypeScript 操作第三方库(Element Plus 的 ElTable)的内部属性
  • 性能优化相关
  • 将 App 安装到 iPhone 真机上测试
  • 安装 Ubuntu Desktop 2504
  • FastChat 架构拆解:打造类 ChatGPT 私有化部署解决方案的基石
  • 深度学习——基于PyTorch的MNIST手写数字识别详解
  • java_oss_微信小程序_通过临时签名url访问oss中存储的图像
  • SpringCloud2020-alibaba
  • 第32节 Node.js 全局对象
  • Mysql官方下载地址
  • TCP的三次握手和四次挥手(面)
  • CFCF2025光连接大会邀请函:昊衡科技诚邀您莅临光纤通信行业盛会,共话未来
  • 我会秘书长杨添天带队赴光明食品集团外高桥食品产业园区考察调研
  • Chainlink VRF 深度解析与实战
  • 智驱未来:迁移科技3D视觉系统重塑复合机器人产业生态
  • 枫清科技携手中化信息挖掘实现AI高价值场景应用,打通智能化“最后一公里”
  • VR 虚拟云展:科技浪潮下的新趋势​
  • java复习 11
  • RDMA简介7之RoCE v2可靠传输