https://leetcode.com/problems/search-a-2d-matrix-ii/
在搜索的时候从右上角开始搜索
假设当前的搜索值为matrix[i][j]
如果当前值小于target则将i++ 搜索下一行
如果当前值大于target则 说明当前列没有target j–
当i 或者j到达了边界 而没有搜索到target则说明没有找到
#include <vector>
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int i, j, row, column;
row = matrix.size();
column = matrix[0].size();
i = 0;
j = column - 1;
while (i < row&&j >= 0)
{
if (matrix[i][j] == target)
return true;
else if (matrix[i][j] < target)
i++;
else
j--;
}
return false;
}
};