Skip to content

Commit c584719

Browse files
committed
solve problem Search A 2d Matrix
1 parent ad4304d commit c584719

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ All solutions will be accepted!
290290
|228|[Summary Ranges](https://leetcode-cn.com/problems/summary-ranges/description/)|[java/py/js](./algorithms/SummaryRanges)|Medium|
291291
|890|[Find And Replace Pattern](https://leetcode-cn.com/problems/find-and-replace-pattern/description/)|[java/py/js](./algorithms/FindAndReplacePattern)|Medium|
292292
|48|[Rotate Image](https://leetcode-cn.com/problems/rotate-image/description/)|[java/py/js](./algorithms/RotateImage)|Medium|
293+
|74|[Search A 2d Matrix](https://leetcode-cn.com/problems/search-a-2d-matrix/description/)|[java/py/js](./algorithms/SearchA2dMatrix)|Medium|
293294

294295
# Database
295296
|#|Title|Solution|Difficulty|

algorithms/SearchA2dMatrix/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Search A 2d Matrix
2+
We can solve this problem by binary search
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public boolean searchMatrix(int[][] matrix, int target) {
3+
int m = matrix.length;
4+
int n = m == 0 ? 0 : matrix[0].length;
5+
if (m == 0 || n == 0)
6+
return false;
7+
8+
int low = 0,
9+
high = m - 1,
10+
rowIndex = -1;
11+
while (low <= high) {
12+
int mid = (low + high) / 2;
13+
if (matrix[mid][n - 1] < target)
14+
low = mid + 1;
15+
else if (matrix[mid][0] > target)
16+
high = mid - 1;
17+
else {
18+
rowIndex = mid;
19+
break;
20+
}
21+
}
22+
if (rowIndex == -1)
23+
return false;
24+
25+
low = 0;
26+
high = n - 1;
27+
while (low <= high) {
28+
int mid = (low + high) / 2;
29+
if (matrix[rowIndex][mid] < target)
30+
low = mid + 1;
31+
else if (matrix[rowIndex][mid] > target)
32+
high = mid - 1;
33+
else
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @param {number} target
4+
* @return {boolean}
5+
*/
6+
var searchMatrix = function(matrix, target) {
7+
let m = matrix.length,
8+
n = m == 0 ? 0 : matrix[0].length
9+
if (m == 0 || n == 0)
10+
return false
11+
12+
let low = 0,
13+
high = m - 1,
14+
rowIndex = -1
15+
while (low <= high) {
16+
let mid = parseInt((low + high) / 2)
17+
if (matrix[mid][n - 1] < target)
18+
low = mid + 1
19+
else if (matrix[mid][0] > target)
20+
high = mid - 1
21+
else {
22+
rowIndex = mid
23+
break
24+
}
25+
}
26+
if (rowIndex == -1)
27+
return false
28+
29+
low = 0
30+
high = n - 1
31+
while (low <= high) {
32+
let mid = parseInt((low + high) / 2)
33+
if (matrix[rowIndex][mid] < target)
34+
low = mid + 1
35+
else if (matrix[rowIndex][mid] > target)
36+
high = mid - 1
37+
else
38+
return true
39+
}
40+
41+
return false
42+
};
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def searchMatrix(self, matrix, target):
3+
"""
4+
:type matrix: List[List[int]]
5+
:type target: int
6+
:rtype: bool
7+
"""
8+
m = len(matrix)
9+
n = 0 if m == 0 else len(matrix[0])
10+
if m == 0 or n == 0:
11+
return False
12+
13+
low = 0
14+
high = m - 1
15+
row_index = -1
16+
while low <= high:
17+
mid = (low + high) / 2
18+
if matrix[mid][n - 1] < target:
19+
low = mid + 1
20+
elif matrix[mid][0] > target:
21+
high = mid - 1
22+
else:
23+
row_index = mid
24+
break
25+
if row_index == -1:
26+
return False
27+
28+
low = 0
29+
high = n - 1
30+
while low <= high:
31+
mid = (low + high) / 2
32+
if matrix[row_index][mid] < target:
33+
low = mid + 1
34+
elif matrix[row_index][mid] > target:
35+
high = mid - 1
36+
else:
37+
return True
38+
39+
return False

0 commit comments

Comments
 (0)