本文主要是介绍Python | Leetcode Python题解之第74题搜索二维矩阵,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution:def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:row,col = len(matrix),len(matrix[0])row_l,row_r = 0,row-1while row_l <= row_r:m = (row_l+row_r)//2if target < matrix[m][0]:row_r = m-1elif target > matrix[m][0]:row_l = m+1elif target == matrix[m][0]:return Trueif row_r == 0 and matrix[row_r][0] > target:return Falsecol_l,col_r = 0,col-1while col_l <= col_r:m = (col_l+col_r)//2if target < matrix[row_r][m]:col_r = m-1elif target > matrix[row_r][m]:col_l = m+1elif target == matrix[row_r][m]:return Truereturn False
这篇关于Python | Leetcode Python题解之第74题搜索二维矩阵的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!