-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathSearchA2DMatrix.java
More file actions
63 lines (54 loc) · 1.57 KB
/
SearchA2DMatrix.java
File metadata and controls
63 lines (54 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @saorav21994
// TC : O(m+n)
// SC : O(1)
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = 0;
if (m != 0) {
n = matrix[0].length;
}
if (m == 0 || n == 0) return false;
int i = 0, j = n-1;
while (j >= 0 && i < m) {
int cur = matrix[i][j];
if (cur == target)
return true;
else if (cur < target) {
i += 1;
}
else
j -= 1;
}
return false;
}
}
// Author: @romitdutta10
// O(Row + logColumn)
// SC: O(1)
// Problem: https://leetcode.com/problems/search-a-2d-matrix/
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int n = matrix.length;
int m = matrix[0].length;
for(int i=0; i<n; i++) {
if(matrix[i][0] <= target && matrix[i][m-1] >= target) {
int low = 0;
int high = m-1;
while(low <= high) {
int mid = low + (high - low)/2;
if(matrix[i][mid] == target) {
return true;
} else if(matrix[i][mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
} else if(matrix[i][0] > target) {
return false;
}
}
return false;
}
}