##题目

##解析
- 解法一:直接把二维列表变为一维列表,然后遍历进行比较
- 解法二:将二位列表使用二分查找来加快效率 “ 0 1 2 3 4 5 6 7 8 9 10 11” i = num //4 j = num % 4;不太稳定不建议使用
##代码
- 解法一
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
for a_list in matrix:
if target in a_list:
return True
return False
-
解法二
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
h = len(matrix)#3
if h == 0:
return False
w = len(matrix[0])#4
if w == 0:
return False
left = 0
right = h*w-1
while left <= right:
mid = (left+right)//2
i = mid //w
j = mid % w
if matrix[i][j] == target:
return True
elif matrix[i][j] > target:
right = mid -1
else:
left = mid=1
else:
return False
-
##运行效果

文章介绍了两种方法处理二维列表并搜索特定目标值:解法一是直接将二维列表转换为一维,适用于简单查找;解法二是使用二分查找技术提高查找效率,适合于较大的矩阵。作者分析了两种方法的适用场景和稳定性。
1103

被折叠的 条评论
为什么被折叠?



