class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
Len=len(matrix)
if Len==0:return False
if Len==1:return self.binarySearch(matrix[0],target)
l=0
r=Len-1
while(l<r):
m=(l+r)/2
if matrix[m][0]<target:
l=m+1
else:
r=m
if matrix[r][0]==target:return True
if r==0:return False
if matrix[r][0]<target:return self.binarySearch(matrix[r],target)
return self.binarySearch(matrix[r-1],target)
def binarySearch(self, s, v):
Len=len(s)
if Len==0:return False
l=0
r=Len-1
while(l<r):
m=(l+r)/2
if s[m]<v:
l=m+1
else:
r=m
if s[r]==v:return True
return False
LeetCode-74-Search a 2D Matrix 二维二分
最新推荐文章于 2022-04-23 08:23:11 发布