编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列
示例:
[
[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
。
给定 target = 20
,返回 false
。
解题思路:首先二维数组有一定的顺序就是列元素的从小到大的排序,
1:先取数组中右上的元素,如果该数字等于要查询的元素,查询过程结束。
2:如果该数字大于要查找的元素,则剔除这个数字所在的列,如果小于该元素剔除该数字所在的行
也就是说如果所要查找的数字不在数组的右上角则每次在查找范围剔除一行或者一列,这样每次可以缩小查询范围,指导查询到要查找的数字 或者查询范围为空
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix==null||matrix.length==0){
return false;
}
int rows=matrix.length;
int colums=matrix[0].length;
boolean isfind=false;
if(matrix!=null&&rows>0&&colums>=0){
int row=0;//第一行开始
int column=colums-1;//最后一列开始
while(row<rows&&column>=0){
if(matrix[row][column]==target){
isfind=true;
break;
}
else if(matrix[row][column]>target){
column–;
}
else{
row++;
}
}
}
return isfind;
}
}
Antabuse Price https://cheapcialisll.com/ – Cialis cialis generique femme purchase cialis online cheap Isotretinoin Purchase On Line Low Price Overseas Chandler
Levitra 5 Mg Tutti I Giorni Wicmymmele https://ascialis.com/# – buy cialis without prescription gaideleplete Real Zentel On Sale affextop where to buy cialis online safely adoreakder Achat Cialis Inde
cialis professional 20 mg Wicmymmele https://asocialiser.com/ – canadian pharmacy cialis 20mg gaideleplete cialis precautions affextop tadalafil cialis from india adoreakder Amoxicillin Kidney Infection
Levitra Durata Effetto Wicmymmele generic cialis 5mg gaideleplete Direct Rx Deal Viagra Generic
https://fcialisj.com/ – where to buy cialis cheap
cialis 5mg
Having read this I thought it was rather enlightening.
I appreciate you taking the time and effort to put this information together.
I once again find myself personally spending a lot of time both reading and leaving comments.
But so what, it was still worth it!