We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 763a6a0 commit 051ddb6Copy full SHA for 051ddb6
Java/Search a 2D Matrix.java
@@ -0,0 +1,27 @@
1
+public class Solution {
2
+
3
+ /**
4
+ * Time: O(log(n*m))
5
+ * Memory: O(1)
6
+ */
7
+ public boolean searchMatrix(int[][] matrix, int target) {
8
+ int n = matrix.length, m = matrix[0].length;
9
+ int left = 0, right = n * m - 1;
10
11
+ while (left <= right) {
12
+ int mid = (left + right) / 2;
13
+ int num = matrix[mid / m][mid % m];
14
15
+ if (num == target)
16
+ return true;
17
18
+ if (num < target) {
19
+ left = mid + 1;
20
+ } else {
21
+ right = mid - 1;
22
+ }
23
24
25
+ return false;
26
27
+}
0 commit comments