【leetcode】Search a 2D Matrix

来源:互联网 发布:东方网络何时复牌 编辑:程序博客网 时间:2024/05/29 11:20
class Solution {public:    bool searchMatrix(vector<vector<int> > &matrix, int target) {        // Note: The Solution object is instantiated only once and is reused by each test case.        int m=matrix.size();        int n=matrix[0].size();                int row=0,col=n-1;//右上角的节点        while(row<m&&col>=0)//O(m+n)        {            if(matrix[row][col]==target)                return true;            else if(matrix[row][col]<target)                row++;            else if(matrix[row][col]>target)                col--;            }        return false;    }};

原创粉丝点击