代码和测试用例:
#include<iostream>
#include<vector>
#include<vector>
using namespace std;
class Soluction
{
public:
bool findNumberIn2DArray(vector<vector<int>>&matrix,int target)
{
int rowSize = matrix.size();
int columnSize = matrix[0].size();
int row = 0;
int column = columnSize-1;
if(matrix.empty())
{
return false;
}
{
public:
bool findNumberIn2DArray(vector<vector<int>>&matrix,int target)
{
int rowSize = matrix.size();
int columnSize = matrix[0].size();
int row = 0;
int column = columnSize-1;
if(matrix.empty())
{
return false;
}
while(row<rowSize&&column>=0)
{
{
if(matrix[row][column]==target)
{
return true;
}
{
return true;
}
else if (matrix[row][column]<target) {
row++;
}
row++;
}
else
{
column--;
}
}
{
column--;
}
}
return false;
}
};
};
int main()
{
std::vector<std::vector<int>> matrix;
Soluction sol;
matrix = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
int target = 7;
cout << boolalpha << sol.findNumberIn2DArray(matrix,target)<<endl;
return 0;
}
{
std::vector<std::vector<int>> matrix;
Soluction sol;
matrix = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
int target = 7;
cout << boolalpha << sol.findNumberIn2DArray(matrix,target)<<endl;
return 0;
}