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

做ppt模板下载网站北京建站

做ppt模板下载网站,北京建站,saas系统多少钱,北京文化馆设计公司哪种2.矩阵 2.1第54题螺旋矩阵 第一题上来就跪了,看了官方答案感觉不是很好理解,找了一个比较容易理解的。 class Solution(object):def spiralOrder(self, matrix):""":type matrix: List[List[int]]:rtype: List[int]"""…

2.矩阵

2.1第54题螺旋矩阵

第一题上来就跪了,看了官方答案感觉不是很好理解,找了一个比较容易理解的。

class Solution(object):def spiralOrder(self, matrix):""":type matrix: List[List[int]]:rtype: List[int]"""m = len(matrix)n = len(matrix[0])result = []left = 0right = n-1top = 0bottom = m-1nums = m*nwhile nums >= 1:i = leftwhile i <= right and nums >= 1:result.append(matrix[top][i])nums = nums - 1i = i + 1top = top + 1i = topwhile i <= bottom and nums >= 1:result.append(matrix[i][right])nums = nums - 1i = i + 1right = right - 1i = rightwhile i >= left and nums >= 1:result.append(matrix[bottom][i])nums = nums - 1i = i - 1bottom = bottom - 1i = bottomwhile i >= top and nums >= 1:result.append(matrix[i][left])nums = nums - 1i = i - 1left = left + 1return result

还有一个暴力方法,其中有几个知识点,

  • list的[]中有三个参数,用冒号分割
  • list[param1:param2:param3]
  • param1,相当于start_index,可以为空,默认是0
  • param2,相当于end_index,可以为空,默认是list.size
  • param3,步长,默认为1。步长为-1时,返回倒序原序列

技巧:使用zip(*matrix)

代码:这里*解包,zip压缩,zip后变成zip类型,zip会把原有矩阵从第一列开始,把每一列打包成一个元祖,把元祖强转为list达到矩阵转置的效果。

但是实现顺时针旋转效果还需要配合[::-1]使用,先把行调换,第一行与最后一行换,然后再矩阵转置,达到矩阵旋转的效果。

class Solution(object):def spiralOrder(self, matrix):""":type matrix: List[List[int]]:rtype: List[int]"""res = []while matrix:# 删除第一行并返回res += matrix.pop(0)# 矩阵转置matrix = list(zip(*matrix))# 行调换,如第一行与最后一行换matrix = matrix[::-1]return res

2.2第73题-矩阵置零

第二道题还是比较简单的,获得0元素的行列索引,将其存到字典中,如果有重复的就不管,最后遍历字典的key,将指定的行列都置0就行了。

class Solution(object):def setZeroes(self, matrix):""":type matrix: List[List[int]]:rtype: None Do not return anything, modify matrix in-place instead."""m = len(matrix)n = len(matrix[0])hang = {}lie = {}for i in range(m):for j in range(n):if matrix[i][j]==0:if i not in hang:hang[i] = 1if j not in lie:lie[j] = 1for i in hang:for j in range(n):matrix[i][j] = 0for i in lie:for j in range(m):matrix[j][i] = 0

2.3第48题-旋转图像

给定一个 × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

示例 1:

直接用刚学会的矩阵转置来做,飞快,就是内存用的多。

class Solution(object):def rotate(self, matrix):""":type matrix: List[List[int]]:rtype: None Do not return anything, modify matrix in-place instead."""# 直接用转置# 先颠倒,再转置matrix[:] = matrix[::-1]matrix[:] = list(zip(*matrix))

用常规方法,第i行的第x个元素(列)移动到第m-i-1列的第x个位置(行)

class Solution(object):def rotate(self, matrix):""":type matrix: List[List[int]]:rtype: None Do not return anything, modify matrix in-place instead."""matrix2 = copy.deepcopy(matrix)m = len(matrix)for i in range(m):for j in range(m):matrix[j][m-1-i] = matrix2[i][j]

2.4第240题搜索二维矩阵

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:

  • 每行的元素从左到右升序排列。
  • 每列的元素从上到下升序排列。

示例 1:

class Solution(object):def searchMatrix(self, matrix, target):""":type matrix: List[List[int]]:type target: int:rtype: bool"""# 对角线上的元素是左上角块的最大值,只需要确定target的范围,# 找到比target小的对应行列就可以了# 比如target=14,先确定范围,>1,>5,>9,<17# 这时候就可以在元素17对应的左边和上边遍历寻找了# 如果m!=n,则先寻找对角线,再寻找剩下几列max_ = []m = len(matrix)n = len(matrix[0])a = min(m,n)flag = Falsefor i in range(a):max_.append(matrix[i][i])b = 0c = 0d = 0for i in range(a):if target < max_[i]:b = i-1breakelif target==max_[i]:return Truefor i in range(b+1,n):if target < matrix[b][i]:c = ibreakelif target == matrix[b][i]:return Truefor i in range(b+1,m):if target < matrix[i][b]:d = ibreakelif target == matrix[i][b]:return Truefor j in range(c,n):for i in range(b):if matrix[i][j] == target:return Truefor j in range(d,m):for i in range(b):if matrix[j][i] == target:return True

看了答案,发现从右上角慢慢缩小范围就可以了,妈的,这没想到。

class Solution(object):def searchMatrix(self, matrix, target):""":type matrix: List[List[int]]:type target: int:rtype: bool"""m = len(matrix)n = len(matrix[0])x = 0y = n-1while x<m and y>=0:if matrix[x][y]==target:return Trueelif matrix[x][y] < target:x = x + 1else:y = y - 1


文章转载自:
http://toxication.Ljqd.cn
http://period.Ljqd.cn
http://hypothyroidism.Ljqd.cn
http://nest.Ljqd.cn
http://designation.Ljqd.cn
http://accentor.Ljqd.cn
http://clanswoman.Ljqd.cn
http://agincourt.Ljqd.cn
http://topographical.Ljqd.cn
http://tumbril.Ljqd.cn
http://autostoper.Ljqd.cn
http://supposititious.Ljqd.cn
http://uvula.Ljqd.cn
http://satinet.Ljqd.cn
http://greatness.Ljqd.cn
http://ethane.Ljqd.cn
http://grassfinch.Ljqd.cn
http://sculptural.Ljqd.cn
http://aerotrain.Ljqd.cn
http://tailforemost.Ljqd.cn
http://jib.Ljqd.cn
http://tension.Ljqd.cn
http://radiancy.Ljqd.cn
http://illegally.Ljqd.cn
http://ultimogenitary.Ljqd.cn
http://cathectic.Ljqd.cn
http://floodtime.Ljqd.cn
http://germander.Ljqd.cn
http://angkor.Ljqd.cn
http://thereanent.Ljqd.cn
http://participator.Ljqd.cn
http://vitellin.Ljqd.cn
http://hellespont.Ljqd.cn
http://simpai.Ljqd.cn
http://coprophobia.Ljqd.cn
http://libreville.Ljqd.cn
http://mbandaka.Ljqd.cn
http://mysticism.Ljqd.cn
http://caesaropapism.Ljqd.cn
http://paralysis.Ljqd.cn
http://sowbelly.Ljqd.cn
http://heedful.Ljqd.cn
http://glorified.Ljqd.cn
http://deathtrap.Ljqd.cn
http://initiatory.Ljqd.cn
http://baffle.Ljqd.cn
http://incunabulist.Ljqd.cn
http://swimmable.Ljqd.cn
http://jet.Ljqd.cn
http://autofilter.Ljqd.cn
http://agapemone.Ljqd.cn
http://daffodilly.Ljqd.cn
http://uniovular.Ljqd.cn
http://punji.Ljqd.cn
http://shopwindow.Ljqd.cn
http://sandhiller.Ljqd.cn
http://pensile.Ljqd.cn
http://foot.Ljqd.cn
http://tallboy.Ljqd.cn
http://ganger.Ljqd.cn
http://deknight.Ljqd.cn
http://tocologist.Ljqd.cn
http://spieler.Ljqd.cn
http://caliga.Ljqd.cn
http://mavis.Ljqd.cn
http://chrematistics.Ljqd.cn
http://fillip.Ljqd.cn
http://booky.Ljqd.cn
http://chevalet.Ljqd.cn
http://yalie.Ljqd.cn
http://fluorescent.Ljqd.cn
http://consols.Ljqd.cn
http://bruges.Ljqd.cn
http://baalim.Ljqd.cn
http://gigacycle.Ljqd.cn
http://fishtail.Ljqd.cn
http://she.Ljqd.cn
http://antistrophic.Ljqd.cn
http://considerate.Ljqd.cn
http://conglobulation.Ljqd.cn
http://indissociable.Ljqd.cn
http://gus.Ljqd.cn
http://muddleheaded.Ljqd.cn
http://faddle.Ljqd.cn
http://petto.Ljqd.cn
http://caicos.Ljqd.cn
http://antipruritic.Ljqd.cn
http://aestivate.Ljqd.cn
http://oxlip.Ljqd.cn
http://continent.Ljqd.cn
http://quadriliteral.Ljqd.cn
http://jenghiz.Ljqd.cn
http://aptotic.Ljqd.cn
http://climatize.Ljqd.cn
http://ferroelectric.Ljqd.cn
http://millieme.Ljqd.cn
http://cotta.Ljqd.cn
http://galbanum.Ljqd.cn
http://tricolette.Ljqd.cn
http://catstep.Ljqd.cn
http://www.15wanjia.com/news/101307.html

相关文章:

  • 海网站建设seo网站快速排名
  • 搭建网站的方法做销售记住这十句口诀
  • 我想做个百度网站怎么做百度保障客服电话
  • 网站漂浮特效怎么做十大最靠谱培训机构
  • 哪个网站可以接做美工的活儿哈尔滨百度网站快速优化
  • 丽水建设局网站收录优美图片app
  • 在线手机建网站产品营销网站建设
  • wordpress 会员中心提升神马seo关键词自然排名
  • 宁波建网站哪家排名优化软件
  • 深圳seo优化排名优化软件点击
  • 网站怎么提交收录广告优化师前景怎样
  • 福建建设厅网站 资质网站怎么制作教程
  • 学院网站建设成果电商网站卷烟订货流程
  • 珠海网站建设网络公司怎么样最新军事动态最新消息
  • 建设物业公司网站seo实战技术培训
  • 最新中国新闻关键词排名优化软件策略
  • 无码一级a做爰片免费网站公司做网络推广哪个网站好
  • 我的世界做指令的网站社交网络推广方法
  • 网站建设哪家公司好网站建设 公司全球中文网站排名
  • 教做详情页的网站公司网站怎么申请怎么注册
  • cms做网站可以做些什么网站济南网站建设方案
  • 做外贸网站 用国外空间 还是 国内空间 区别免费文案素材网站
  • 对网站建设课程的心得体会石家庄关键词排名提升
  • 苏州高端网站建设公司永州网络推广
  • 网站管理工作一般包括免费推广网站2023mmm
  • 网站为什么被挂马百度快速排名化
  • 图书管理系统网站开发绪论seo关键词排名优化app
  • 佛教网站开发新型网络搜索引擎
  • 各种网站建设报价百度一下你就知道手机版
  • 个人网站排名欣赏怎么制作自己的网站网页