如何在有序矩阵中高效搜索?
我有一个 x
by y
矩阵,其中每行和每列均按升序排列,如下所示。
1 5 7 9
4 6 10 15
8 11 12 19
14 16 18 21
如何在这个矩阵中搜索 O(x+y)
中的数字?
我面试时被问到这个问题,但不知道怎么回答。很想知道是否可以做到。
I have a x
by y
matrix, where each row and each column are in ascending order as given below.
1 5 7 9
4 6 10 15
8 11 12 19
14 16 18 21
How to search this matrix for a number in O(x+y)
?
I was asked this question for an interview, but could not figure out the way. Curious to know if it could be done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从第一行的最后一个元素(右上角)开始。
将其与
键
进行比较。我们有 3 种情况:如果它们相等,我们就完成了。
如果
key
大于该元素那么这意味着
key
不能存在在该行中移动搜索
到其下面的元素。
如果
key
小于该元素,则这意味着
key
可能存在于其中向左行并且不能出现在更下方的列中,因此将搜索移动
到它左边的元素。
继续这样做,直到找到该元素,否则您无法进一步移动(键不存在)。
伪代码:
Start at the last element of the first row(top-right corner).
Compare it with the
key
. We have 3 cases:If they are equal we are done.
If
key
is greater than that elementthen it means
key
cannot be presentin that row so move the search
to the element below it.
If
key
is less than that element thenit means
key
could be present in thatrow towards left and cannot be present in the column further down, so move the search
to the element left of it.
Keep doing it till you find the element or you cannot further move(key does not exist).
Pseudo code:
将矩阵拆分为 4 个子矩阵。如果子矩阵的右下角小于 key,则丢弃它。如果子矩阵的左上角大于键,则丢弃它。对剩余的子矩阵重复分裂过程。
[更新]
对于一些伪代码(以及复杂性的讨论),请参阅 Jeffrey L Whitledge 的回答 这个问题。
Split the Matrix in 4 submatrices. If bottom right of a sub-matrix is less than key, discard it. If the top left of a sub-matrix is bigger than the key, discard it. Repeat the splitting procedure for the remaining sub-matrices.
[Update]
For some pseudo code (and a discussion of complexity) see Jeffrey L Whitledge's answer of this question.
实际上,我们可以使用两次二分查找,先通过二分查找找到目标行,然后通过二分查找找到该行中的目标,所以时间复杂度为 O(lgx) + O(lgy),即 O(lgx + lgy) ),O(x+y) 更好。
Actually, we can use binary search twice, find the target row by binary search first, then find the target in the row by binary search, so the time complexity is O(lgx) + O(lgy), is O(lgx + lgy), better the O(x+y).