---
# System prepended metadata

title: 0074. Search a 2D Matrix
tags: [Leetcode, FaceBook, Binary Search, Medium]

---

# 0074. Search a 2D Matrix
###### tags: `Leetcode` `Medium` `FaceBook` `Binary Search`
Link: https://leetcode.com/problems/search-a-2d-matrix/
## 思路 O(log(M*N))

直接把它看成flatten的sorted array就可以了，因为上一行的最大值一定比下一行最开始的值小
## Code
```java=
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        
        int l = 0, r = m*n;
        while(l<r){
            int mid = l+(r-l)/2;
            int curr = matrix[mid/n][mid%n];
            if(curr < target){
                l = mid+1;
            }
            else if(curr==target){
                return true;
            }
            else{
                r = mid;
            }
        }
        return false;
    }
}
```