> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/binary-search/search-a-2d-matrix-ii.md).

# Search a 2D Matrix II

{% embed url="<https://www.lintcode.com/problem/38/>" %}

## Solution 1 (Java)

```java
public class Solution {
    /**
     * @param matrix: A list of lists of integers
     * @param target: An integer you want to search in matrix
     * @return: An integer indicate the total occurrence of target in the given matrix
     */
    public int searchMatrix(int[][] matrix, int target) {
        int row = 0, col = matrix[0].length - 1;
        int targetCount = 0;
        while (row < matrix.length && col >= 0) {
            if (matrix[row][col] == target) {
                targetCount++;
                row++;
                col--;
            } else if (matrix[row][col] < target) {
                row++;
            } else {
                col--;
            }
        }
        return targetCount;
    }
}
```

### Notes

* We pick a special number in the 2D matrix here. Either the **top right** element or the **bottom left** element will work. The reason is that we want to reduce the range in which we research for the `target`. In the code above, we chose the top right element and it is the largest in its row but the smallest in its column. Thus, if the `target` is larger than it, we proceed downward to find it; if the `target` is smaller than it, we proceed to the left. And if the `target` is equal to it, we increment the `targetCounter` and move diagonally since each number is unique in its row and column.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://blog.yushunchen.com/algo/binary-search/search-a-2d-matrix-ii.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
