1、题目来源
2、题目描述
编写一个高效的算法来搜索 m x
n 矩阵 matrix
中的一个目标值 target
。该矩阵具有以下特性:
- 每行的元素从左到右升序排列。
- 每列的元素从上到下升序排列。
示例 1:
输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
输出:true
示例 2:
输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
输出:false
提示:
m == matrix.length
n == matrix[i].length
1 <= n, m <= 300
-109 <= matrix[i][j] <= 109
- 每行的所有元素从左到右升序排列
- 每列的所有元素从上到下升序排列
-109 <= target <= 109
3、题解分享
java
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
// 思路:直接遍历
for (int[] row : matrix) {
for (int element : row) {
if (element == target) {
return true;
}
}
}
return false;
}
}
java
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
//思路:二分查找 + 每行进行二分搜索
for (int[] row : matrix) {
int index = search(row, target);
if (index >= 0) {
return true;
}
}
return false;
}
public int search(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low <= high) {
int mid = (high - low) / 2 + low;
int num = nums[mid];
if (num == target) {
return mid;
} else if (num > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
}
java
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
// 思路:直接搜索 + 从左下角或者右上角 往对角线方向搜索
int n = matrix.length;
int m = matrix[0].length;
int row = n -1,col = 0;
while(row >=0 && col < m){
if(matrix[row][col] == target){
return true;
}else if(matrix[row][col] > target){
--row;
}else{
++col;
}
}
return false;
}
}