Search a 2D Matrix LeetCode Solution

Last updated on October 25th, 2024 at 10:26 pm

Here, We see Search a 2D Matrix LeetCode Solution. This Leetcode problem is done in many programming language like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Array, Binary Search

Level of Question

Medium

Search a 2D Matrix LeetCode Solution

Search a 2D Matrix LeetCode Solution

Problem Statement

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.
Example 1:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false

1. Search a 2D Matrix Leetcode Solution C++

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
    if(matrix.empty() || matrix[0].empty())
    {
        return false;
    }
    int m = matrix.size(), n = matrix[0].size();
    int start = 0, end = m*n - 1;
    while(start <= end)
    {
        int mid = start + (end - start)/2;
        int e = matrix[mid/n][mid%n];
        if(target < e)
        {
            end = mid - 1;
        }
        else if(target > e)
        {
            start = mid + 1;
        }
        else
        {
            return true;
        }
    }
    return false;        
    }
};

2. Search a 2D Matrix Leetcode Solution Java

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
            int i = 0, j = matrix[0].length - 1;
            while (i < matrix.length && j >= 0) {
                    if (matrix[i][j] == target) {
                        return true;
                    } else if (matrix[i][j] > target) {
                        j--;
                    } else {
                        i++;
                    }
                }
            
            return false;        
    }
}

3. Search a 2D Matrix Leetcode Solution JavaScript

var searchMatrix = function(matrix, target) {
  if (!matrix.length || !matrix[0].length) return false;

  let row = 0;
  let col = matrix[0].length - 1;

  while (col >= 0 && row <= matrix.length - 1) {
    if (matrix[row][col] === target) return true;
    else if (matrix[row][col] > target) col--;
    else if (matrix[row][col] < target) row++;
  }

  return false;    
};

4. Search a 2D Matrix Leetcode Solution Python

class Solution(object):
    def searchMatrix(self, matrix, target):
        if not matrix:
            return False
        row = len(matrix)       # Number of Rows of the matrix...
        col = len(matrix[0])    # Number of Columns of the matrix...
        beg = 0
        end = row*col
        while beg < end:
            mid = beg + (end - beg) // 2
            idx = matrix[mid / col][mid % col];
            if idx == target:
                return True
            if idx < target:
                beg = mid + 1
            else:
                end = mid
        return False 
Scroll to Top