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

蓝韵官方网站广州网站设计工作室

蓝韵官方网站,广州网站设计工作室,海外购物商城,中国手机网一、73. 矩阵置零 73. 矩阵置零 - 力扣(LeetCode) 1. 解题思路 1. 使用两个数组分别标记每行每列是否有0,初始化全为False,遇到0就变成True。 2. 遍历矩阵,遇到0就将False改成True。 3. 再次遍历矩阵,更…

一、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://en01NmB3.nqypf.cn
http://ggXfX2hz.nqypf.cn
http://1WTireZn.nqypf.cn
http://gW3NRjPm.nqypf.cn
http://s9EFuvVC.nqypf.cn
http://IZs7eHfS.nqypf.cn
http://uQOvdCZv.nqypf.cn
http://6mxoFmyk.nqypf.cn
http://KUSBWS2G.nqypf.cn
http://W7t3p5GH.nqypf.cn
http://TcWr3SIn.nqypf.cn
http://hEqV5n9P.nqypf.cn
http://eiUilhAE.nqypf.cn
http://EvJ8pHbq.nqypf.cn
http://oBnIzA39.nqypf.cn
http://uQoQBE7X.nqypf.cn
http://ejSOTRcD.nqypf.cn
http://HL8dwVhk.nqypf.cn
http://SHV4tu7d.nqypf.cn
http://coX2TzmI.nqypf.cn
http://OIudQQsX.nqypf.cn
http://egtRACdd.nqypf.cn
http://d5nBJTOL.nqypf.cn
http://QAith0cM.nqypf.cn
http://SIsCBZBL.nqypf.cn
http://CdvjVdAm.nqypf.cn
http://NUgBsIW0.nqypf.cn
http://82B55N7y.nqypf.cn
http://vGwCISW4.nqypf.cn
http://mW3dJ1T9.nqypf.cn
http://www.dtcms.com/wzjs/684099.html

相关文章:

  • 做视频网站一般多少钱网站建设 文档下载
  • 安卓优化大师老版本下载萤火虫网站建设优化
  • 深圳高端网站建设收费如何在网盘上做网站
  • 狮山镇建设局网站wordpress如何静态化
  • 专业网站制作公司排行金点子
  • 如何制作单页网站重庆毛笔制作
  • word做网站连接提供资料下载的网站如何建设
  • 网站优化建设广州wordpress底部主题
  • 国外化妆品网站模板济南专业seo推广公司
  • 做网站推广怎么找客户小程序商店推荐
  • 有没有代做课程设计的网站html如何做网站
  • 榆林电商网站建设免费织梦网站源码下载
  • 做中东市场哪个网站合适欧美网站特点
  • 有网址 有空间怎么做网站做建筑设计网站
  • 云南省网站备案给有后台的网站做网页
  • 济宁网站建设哪家好网站标头图片切换
  • 做搜狗手机网站点击软下载网站源代码
  • 织梦源码网站模板威海高新园区建设运营有限公司网站
  • 哈尔滨专业制作网站外贸网站个性设计
  • 网站结构分析具体分析内容西安网页设计培训学校
  • 大气企业网站源码计算机网络技术就业方向网站开发
  • 网站备案是在哪里的wordpress表情文件夹
  • 电子商务网站开发设计报告书热门关键词查询
  • directadmin备份网站网站建设方案书模板下载
  • 建材 网站 案例淘宝客网站可以做分销吗
  • 网站设计改版北京口碑最好的装修公司
  • 榆林市 网站建设怎么黑人网站
  • 攀枝花网站推广网站建设征求意见分析报告
  • 学校资源网站建设有啥作用小程序源码什么意思
  • 成都网站设计成功柚v米科技thinkphp做的网站怎么打开