# 74. Search a 2D Matrix

{% hint style="success" %}
Find possible row, then apply binary search

Consider edge case
{% endhint %}

{% tabs %}
{% tab title="Java" %}

```java
// Some code
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = 0;
        for(; row < matrix.length; row ++) {
            int temp = matrix[row][0];
            if(temp == target)
                return true;
            else if(temp > target)
                break;
        }
        
        row = row == 0? 0: row - 1;
        int left = 0, right = matrix[row].length - 1, temp;
        while(left <= right) {
            int mid = left + (right - left)/2;
            temp = matrix[row][mid];
            if(temp == target)
                return true;
            else if(temp < target)
                left = mid + 1;
            else
                right = mid - 1;
        }
        
        return false;
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r24zeng.gitbook.io/leetcode-notebook/wan-quan-an-zhao-jiu-zhang-suan-fa-shua-de-60-dao-zuo-you/binary-search/74.-search-a-2d-matrix.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
