标签:sea color matrix als style targe pre bsp span
方法:采用二分查找的方法,注意rt = rows*cols-1;
class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { if(matrix.size() == 0 || matrix[0].size() == 0) return false; int rows = matrix.size(), cols = matrix[0].size(); int lt = 0, rt = rows * cols-1; while(lt <= rt) { int mid = (lt + rt) / 2; int value = matrix[mid/cols][mid%cols]; if(target == value) return true; if(target > value) lt = mid + 1; else rt = mid - 1; } return false; } };
标签:sea color matrix als style targe pre bsp span
原文地址:http://www.cnblogs.com/chengyuz/p/6819718.html