Leetcode Problem 74. Search a 2D Matrix

74. Search a 2D Matrix

Leetcode Solutions

Binary Search onD Matrix

  1. Initialize left to 0 and right to m * n - 1.
  2. While left is less than or equal to right: a. Calculate mid as the average of left and right (integer division). b. Calculate the row index as mid // n and the column index as mid % n. c. If the element at matrix[row][col] is equal to target, return true. d. If the element is less than target, move left to mid + 1. e. If the element is greater than target, move right to mid - 1.
  3. If the target is not found, return false.
UML Thumbnail

Stepwise Linear Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...