Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a715532

Browse files
solves search a 2d matrix
1 parent 99d65bc commit a715532

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [![Java](assets/java.png)](src/ClimbingStairs.java) [![Python](assets/python.png)](python/climbing_stairs.py) | |
6969
| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path) | [![Java](assets/java.png)](src/SimplifyPath.java) | |
7070
| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes) | [![Java](assets/java.png)](src/SetMatrixZeroes.java) | |
71+
| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix) | [![Java](assets/java.png)](src/SearchA2DMatrix.java) | |
7172
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
7273
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
7374
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |

src/SearchA2DMatrix.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/search-a-2d-matrix
2+
// T: O(log(m*n))
3+
// S: O(1)
4+
5+
public class SearchA2DMatrix {
6+
public boolean searchMatrix(int[][] matrix, int target) {
7+
final int rows = matrix.length, columns = matrix[0].length;
8+
int left = 0, right = rows * columns - 1, middle;
9+
while (left <= right) {
10+
middle = left + (right - left) / 2;
11+
if (matrix[middle / columns][middle % columns] == target) return true;
12+
else if (matrix[middle / columns][middle % columns] > target) right = middle - 1;
13+
else left = middle + 1;
14+
}
15+
return false;
16+
}
17+
}

0 commit comments

Comments
 (0)