leetcode 刷题 73 74

举报
毛利 发表于 2021/07/15 08:23:56 2021/07/15
【摘要】 class Solution: def setZeroes(self, matrix): # First row has zero? m, n, firstRowHasZero = len(matrix), len(matrix[0]), not all(matrix[0]) # Use first row/column as marker, scan the ...

在这里插入图片描述

在这里插入图片描述

class Solution: def setZeroes(self, matrix): # First row has zero? m, n, firstRowHasZero = len(matrix), len(matrix[0]), not all(matrix[0]) # Use first row/column as marker, scan the matrix for i in range(1, m): for j in range(n): if matrix[i][j] == 0: matrix[0][j] = matrix[i][0] = 0 # Set the zeros for i in range(1, m): for j in range(n - 1, -1, -1): if matrix[i][0] == 0 or matrix[0][j] == 0: matrix[i][j] = 0 # Set the zeros for the first row if firstRowHasZero: matrix[0] = [0] * n

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述
和我想得一样直接遍历判断
在这里插入图片描述

二分查找

class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if(matrix == None or len(matrix) == 0 or len(matrix[0]) == 0): return False row = len(matrix) column = len(matrix[0]) i = row-1 j = 0 while i >= 0: if(matrix[i][0] > target): i = i-1 else: break while j+1 < column: if(matrix[i][j] < target): j = j+1 else: break if(matrix[i][j] == target): return True return False

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

文章来源: maoli.blog.csdn.net,作者:刘润森!,版权归原作者所有,如需转载,请联系作者。

原文链接:maoli.blog.csdn.net/article/details/90525891

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。