/*
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
*/
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int nRow = array.size() ;
if(nRow <= 0) return false ;
int nCol = array[0].size() ;
if(nCol <= 0) return false ;
int nCurRowIndex = 0 ;
int nCurColIndex = nCol - 1 ; // begin with right top
while(nCurRowIndex < nRow && nCurColIndex >= 0) {// while end condition
int nTestNum = array[nCurRowIndex][nCurColIndex] ;
if(target < nTestNum) {
nCurColIndex-- ;
}else if(target > nTestNum) {
nCurRowIndex++ ;
}else {
return true ;
}
}
return false ;
}
};
二维数组中的查找
最新推荐文章于 2021-10-20 18:45:59 发布