编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
- 每行中的整数从左到右按升序排列。
- 每行的第一个整数大于前一行的最后一个整数。
示例 1:
输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 输出: true
示例 2:
输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 输出: false
题目标签:Array / Binary Search
题目链接:LeetCode / LeetCode中国
这题很显然用二分法。
用ruby的话,可以一行搞定~
Language | Runtime | Memory |
---|---|---|
java | 0 ms | 38.6 MB |
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix.length == 0 || matrix[0].length == 0) return false;
int l = 0, r = matrix.length - 1;
while (l < r) {
int m = l + r + 1 >> 1;
if (matrix[m][0] <= target) l = m;
else r = m - 1;
}
int i = l;
l = 0; r = matrix[0].length - 1;
while (l <= r) {
int m = l + r >> 1;
if (matrix[i][m] == target) return true;
if (matrix[i][m] > target) r = m - 1;
else l = m + 1;
}
return false;
}
}
Language | Runtime | Memory |
---|---|---|
ruby | 52 ms | N/A |
# @param {Integer[][]} matrix
# @param {Integer} target
# @return {Boolean}
def search_matrix(matrix, target)
return matrix.flatten.include?(target)
end