Leetcode 74. Search a 2D Matrix

第74題,問題如下: 

You are given an m x n integer matrix matrix with the following two properties:

  • Each row is sorted in non-decreasing order.
  • The first integer of each row is greater than the last integer of the previous row.

Given an integer target, return true if target is in matrix or false otherwise.

You must write a solution in O(log(m * n)) time complexity.

 

Example 1:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false

 

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104

查找在2維陣列中的某個元素,找row時用商(mid/n),找column時用餘數來定位(mid%n)。

找到時回傳true,否則回傳false。以mid來協助定位index。

“`

// Q74: Search a 2D Matrix
public boolean searchMatrix(int[][] matrix, int target) {
    if (matrix.length == 0)
        return false;

    final int m = matrix.length;
    final int n = matrix[0].length;
    int l = 0;
    int r = m * n;

    while (l < r) {
        final int mid = (l + r) / 2;
        final int i = mid / n;
        final int j = mid % n;
        if (matrix[i][j] == target)
            return true;
        if (matrix[i][j] < target)
            l = mid + 1;
        else
            r = mid;
    }

    return false;
}

“`

Leave a Reply