74. Search a 2D Matrix
Posted 业精于勤荒于嬉,行成于思毁于随
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了74. Search a 2D Matrix相关的知识,希望对你有一定的参考价值。
class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); if (!m) { return false; } int n = matrix.at(0).size(); if (!n) { return false; } int row = -1; for (int i = 0; i < m; i++) { if (matrix[i][0] <= target && matrix[i][n - 1] >= target) { row = i; break; } } if (row < 0) { return false; } //二分查找 int low = 0, high = n - 1; vector<int> tVec = matrix.at(row); while (low <= high) { int mid = (low + high) / 2; if (tVec.at(mid) > target) { high = mid - 1; } else if (tVec.at(mid) < target){ low = mid + 1; } else { return true; } } return false; } };
以上是关于74. Search a 2D Matrix的主要内容,如果未能解决你的问题,请参考以下文章