---
# System prepended metadata

title: Leetcode 74. Search 2D matrix

---

# Leetcode 74. Search 2D matrix

## 逐行二分查找

```python=
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        xc, yc = len(matrix[0]), len(matrix)

        def binarySearch(nums: List[int], target: int, length: int):
            left = 0
            right = length - 1
            while left <= right:
                mid = left + (right - left) // 2
                if nums[mid] < target:
                    left = mid + 1
                elif nums[mid] > target:
                    right = mid - 1
                else:
                    return True
            return False
        
        for y in range(yc):
            if binarySearch(matrix[y],target, xc):
                return True
        return False
```
