思路:
这是经典的,矩阵寻值模型,从右上角或者左下角 往对角走即可
假设现在是从右上角往左下角走:
- 当 plants[i][j] $>$ target 时 ,j–
- 当 plants[i][j] $<$ target 时 ,i++
- 当 plants[i][j] $=$ target 时,返回 true
class Solution {
public:
bool findTargetIn2DPlants(vector<vector<int>>& plants, int target) {
int m=(int)plants.size();
if(m==0)return false;
int n=(int)plants[0].size();
int x=0,y=n-1;
while(x<m&&y>=0)
{
if(plants[x][y]>target)y--;
else if(plants[x][y]<target)x++;
else return true;
}
return false;
}
};
//1 3 5
//2 5 7