---
# System prepended metadata

title: 240-Search a 2D Matrix II
tags: [Medium]

---

# 240-Search a 2D Matrix II
###### tags: `Medium`
## Question
https://leetcode.com/problems/search-a-2d-matrix-ii/
## Key
## Reference
## Solution


>related problem : 
### My Solution
```java=
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        return search(matrix, target, matrix.length - 1, 0);
    }
    
    public boolean search(int[][] matrix, int target, int row, int start) {
        boolean res = BS(matrix[row], target, start);
        if(row == 0 || res)
            return res;
        while(start < matrix[0].length - 1 && matrix[row][start + 1] < target)
            start++;
        return search(matrix, target, row - 1, start);
    }
    
    public boolean BS(int[] nums, int target, int start) {
        int l = start;
        int r = nums.length - 1;
        while(l <= r) {
            int mid = l + (r - l) / 2;
            if(nums[mid] > target)
                r = mid - 1;
            else if(nums[mid] < target)
                l = mid + 1;
            else 
                return true;
        }
        return false;
    }
}
```
### Other Solution
```java=
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length - 1;
        int col = 0;
        
        while(row >=0 && col < matrix[0].length) {
            if(matrix[row][col] > target) {
                row--;
            } else if (matrix[row][col] < target) {
                col++;
            } else {
                //found it
                return true;
            }
        }
        return false;
    }
}
```