Created
January 1, 2026 14:43
-
-
Save rtsoy/3268db23c076e26f60f56a174e5dab6c to your computer and use it in GitHub Desktop.
74. Search a 2D Matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://leetcode.com/problems/search-a-2d-matrix/ | |
| // | |
| // Time: O(log(m*n)) | |
| // Space: O(1) | |
| // | |
| // m = number of rows | |
| // n = number of columns | |
| // .................... // | |
| func searchMatrix(matrix [][]int, target int) bool { | |
| m, n := len(matrix), len(matrix[0]) | |
| lo, hi := 0, m*n-1 | |
| for lo <= hi { | |
| mi := lo + (hi-lo)/2 | |
| row, col := mi/n, mi%n | |
| candidate := matrix[row][col] | |
| if candidate == target { | |
| return true | |
| } | |
| if candidate > target { | |
| hi = mi - 1 | |
| } else { | |
| lo = mi + 1 | |
| } | |
| } | |
| return false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment