Search in Matrix

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true
class Solution:
def searchMatrix(self, matrix, target):
if not matrix or not matrix[0]:return False
C = len(matrix[0])
for row in matrix:
i = bisect.bisect_left(row, target)
if i==C:continue
if target==row[i]:return True
return False
class Solution:
def searchMatrix(self, matrix, target):
if not matrix or not matrix[0]:return False
j = -1
for row in matrix:
while j+len(row) and row[j]>target:j-=1
if target==row[j]:return True
return False
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix or not matrix[0]:return False
R, C =len(matrix), len(matrix[0])
def binary():
l, r = 0, R*C-1
while l<=r:
m = (l+r)//2
mid_val = matrix[m//C][m%C]
if mid_val==target:return True
elif mid_val<target:l+=1
else:r-=1
return False
return binary()
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
class Solution:
def searchMatrix(self, matrix, target):
if not matrix or not matrix[0]:return False
C = len(matrix[0])
for row in matrix:
i = bisect.bisect_left(row, target)
if i==C:continue
if target==row[i]:return True
return False
class Solution:
def searchMatrix(self, matrix, target):
if not matrix or not matrix[0]:return False
j = -1
for row in matrix:
while j+len(row) and row[j]>target:j-=1
if target==row[j]:return True
return False

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store