题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
C++11(clang++ 3.9)
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
// 1 3 4 5
// 2 5 8 9
// 5 6 11 13
// 8 9 12 15
// test case: target = 11
int row_count = array.size();
if(row_count <= 0)
return false;
int col_count = array[0].size();
if(col_count <= 0)
return false;
int row = 0;
int col = col_count - 1;
while(row < row_count && col >= 0)
{
if(array[row][col] == target)
return true;
else if(array[row][col] < target)
row++;
else
col--;
}
return false;
}
};
来源:https://www.cnblogs.com/hotwater99/p/12432539.html