Search a 2D Matrix

 bool searchMatrix(const vector<vector<int>> &matrix, int target)
      {
          int m = matrix.size();//
          int n = matrix.front.size();
          
          int first = 0;
          int last = m*n;
          while (first < last)
          {
              int mid = first + (last - first) / 2;
              //确定其中矩阵中的位置
              int value = matrix[mid / n][mid%n];
              if (value == target)return true;
              else if (target>value)
                  first = mid + 1;
              else
                  last = mid;

          }
      }
View Code

 

 

posted @ 2016-07-16 14:43  牧马人夏峥  阅读(104)  评论(0编辑  收藏  举报