74. Search a 2D Matrix (Medium)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

Solution 1: 9ms

把矩阵的各行连接成一个数列,然后用二分查找法。这样的复杂度是O(log(m*n))。

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.empty() || matrix[0].empty()) return false;
        int m = matrix.size(), n = matrix[0].size();
        int i = 0, j = m*n-1;
        while (i <= j) {
            int mid = i+(j-i)/2;
            int x = mid/n, y = mid%n;
            if (matrix[x][y] == target) return true;
            else if (target < matrix[x][y]) --j;
            else ++i;
        }
        return false;
    }
};
public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) return false;
        int m = matrix.length, n = matrix[0].length;
        int l = 0, r = m*n-1;
        while (l <= r) {
            int mid = (l+r)/2;
            int row = mid/n, col = mid%n;
            if (matrix[row][col] < target) l = mid+1;
            else if (matrix[row][col] > target) r = mid-1;
            else return true;
        }
        return false;
    }
}

Solution 2:

现在考虑先用二分查找法确定target在哪一行,然后对该行进行二分查找,这样复杂度为$$O(logm + logn)$$,和解法一一样。

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) return false;
        int m = matrix.length, n = matrix[0].length;
        int l = 0, r = m-1;
        while (l+1 < r) { // at least 3 items
            int mid = (l+r)/2;
            if (matrix[mid][0] < target) l = mid;
            else if (matrix[mid][0] > target) r = mid;
            else return true;
        }

        int row;
        if (matrix[l][n-1] > target) row = l;
        else if (matrix[l][n-1] < target) row = r;
        else return true;

        l = 0; r = n-1;

        while (l <= r) {
            int mid = (l+r)/2;
            if (matrix[row][mid] < target) l = mid+1;
            else if (matrix[row][mid] > target) r = mid-1;
            else return true;

        }
        return false;
    }
}

results matching ""

    No results matching ""