---
# System prepended metadata

title: Leetcode 74. Search 2D Martix
tags: [Martix, Leetcode, Binary Search]

---

## 題解

### 使用 Flatten 把二維轉換成一維進行二分搜索

```python=
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        m,n = len(matrix),len(matrix[0])
        length = m * n
        left, right = 0, length - 1

        while left <= right:
            mid = left + (right - left) // 2
            cur = matrix[mid // n][mid % n]
            if cur > target:
                right = mid - 1
            elif cur < target:
                left = mid + 1
            else:
                return True
        return False
```