Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
Given target = 3
, return true
思路:1.二分比较判断可能在哪一行,确定searchRow,再在searchRow中二分找
2. 当成线性表,来二分找。
注意:这两个的复杂度???? 1.O(lgm+lgn) 2.O(lg(m*n))
class Solution {public: bool searchMatrix(vector> &matrix, int target) { if(matrix.empty()) return false; int row = matrix.size(); int rowA=0; int rowB = row-1; int rowMid=0; int searchRow; while(rowA =matrix[rowMid-1][0]){searchRow=rowMid-1;break;} else rowB=rowMid-2; }else{ if(target matrix[searchRow][colB]) return false; while(colA<=colB){ colMid = (colA+colB)/2; if(target==matrix[searchRow][colMid]) return true; if(target