English 中文(简体)
如何在订购的矩阵中有效搜索? [复制]
原标题:How to efficiently search in an ordered matrix? [duplicate]

页: 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)。





相关问题
How to add/merge several Big O s into one

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 ...

Grokking Timsort

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 ...

Manually implementing high performance algorithms in .NET

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, ...

Print possible strings created from a Number

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->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

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 ...

Quick padding of a string in Delphi

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 "...

热门标签