Advertisement
lazar955

Untitled

Oct 22nd, 2023
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.54 KB | None | 0 0
  1. func searchMatrix(matrix [][]int, target int) bool {
  2.     if len(matrix) == 0 {
  3.         return false
  4.     }
  5.     m, n := len(matrix), len(matrix[0])
  6.     left, right := 0, m*n-1
  7.  
  8.     for left <= right {
  9.         mid := left + (right-left)/2
  10.         mid_row := mid / n
  11.         mid_col := mid - mid_row*n
  12.  
  13.         if matrix[mid_row][mid_col] == target {
  14.             return true
  15.         } else if matrix[mid_row][mid_col] < target {
  16.             left = mid + 1
  17.         } else {
  18.             right = mid - 1
  19.         }
  20.     }
  21.     return false
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement