# Search in Matrix

`Input:matrix = [  [1,   3,  5,  7],  [10, 11, 16, 20],  [23, 30, 34, 50]]target = 3Output: 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`

--

--

## More from Jimmy (xiaoke) Shen

Data Scientist/MLE/SWE @takemobi

Love podcasts or audiobooks? Learn on the go with our new app.