#include <vector>
using namespace std;
class Solution {
private:
bool search(vector<vector<int>>&ma, int start, int end, int target,int&column)
{
if (end < start)
return false;
int mid = (start + end) / 2;
int mrow, mcolumn,temp;
mrow = mid / column;
mcolumn = mid%column;
temp = ma[mrow][mcolumn];
if (temp == target)
return true;
else if (target < temp)
return search(ma, start, mid - 1, target, column);
else
return search(ma, mid + 1, end, target, column);
}
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int row, column;
row = matrix.size();
column = matrix[0].size();
return search(matrix, 0, row*column - 1, target,column);
}
};
Search a 2D Matrix
最新推荐文章于 2022-06-15 11:43:15 发布