页: 1
1 5 7 9
4 6 10 15
8 11 12 19
14 16 18 21
如何在<条码>O(x+y)条码>中查找这一矩阵?
我被问及这个问题,但不能说出。 想知道是否可以这样做。
Start at the last element of the first row(top-right corner).
Compare it with the key
. We have 3 cases:
如果它们是平等的,我们就这样做了。
If key
is greater than that element
then it means key
cannot be present
in that row so move the search
to the element below it.
If key
is less than that element then
it means key
could be present in that
row towards left and cannot be present in the column further down, so move the search
to the element left of it.
坚持这样做,直到你找到这个要素,或者你不能再走(根本不存在)。
Pseudo Code:
Let R be number of rows
Let C be number of columns
Let i = 0
Let j = C-1
found = false
while( i>=0 && i<R) && (j>=0 && j<C) )
if (matrix[i][j] == key )
found = true
break
else if( matrix[i][j] > key )
j--
else if( matrix[i][j] < key )
i++
end-while
在4个次级矩阵中提供矩阵。 如果次矩阵的底层权利低于关键,则予以放弃。 如果一个子矩阵的顶部大于钥匙,就予以抛弃。 答复其余分矩阵的分选程序。
[Update] For some pseudo code (and a discussion of complexity) see Jeffrey L Whitledge s answer of this question.
// the matrix is like this, from left to right is ascending, and
// from down to up is ascending, but the second row start is not always bigger than the first row s end, which is diff from [leetcode]https://oj.leetcode.com/problems/search-a-2d-matrix/
// 1 5 7 9
// 4 6 10 15
// 8 11 12 19
// 14 16 18 21
// time complexity is O(x+y), x is the count of row, and y is the count of column
public boolean searchMatrix2(int[][] matrix, int target) {
int rowCount = matrix.length;
if(rowCount == 0) return false;
int colCount = matrix[0].length;
if(colCount == 0) return false;
//first find the target row, needs O(x)
int targetRow = 0;
while(targetRow < rowCount-1 && matrix[targetRow+1][0] <= target) {
targetRow++;
}
//than find the target in the target row, needs O(y), so the total is O(x)+O(y)
boolean result = false;
for(int i = 0; i < colCount; i ++) {
if(matrix[targetRow][i] == target) {
result = true;
break;
}
}
return result;
}
实际上,我们可以使用双轨搜索器,首先通过双轨搜索找到目标行,然后通过双轨搜索在浏览中找到目标,因此时间复杂性是O(lgx)+O(lgy),即O(lgx + lgy),即O(x+y)。
If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...
I m using Electro in Lua for some 3D simulations, and I m running in to something of a mathematical/algorithmic/physics snag. I m trying to figure out how I would find the "spin" of a sphere of a ...
There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...
As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...
Given an array of integers arr = [5, 6, 1]. When we construct a BST with this input in the same order, we will have "5" as root, "6" as the right child and "1" as left child. Now if our input is ...
I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...