[leetcode]Search a 2D Matrix

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(matrix.empty()) return false;
        
        int M = matrix.size();
        int N = matrix[0].size();
        
        int i = 0, j = N-1;
        
        while( i < M && j >=0){
            if(matrix[i][j] == target) return true;
            else if(matrix[i][j] < target) i++;
            else j--;
        }
        return false;
        
    }
};


posted @ 2013-07-24 19:25  爱生活,爱编程  阅读(262)  评论(0编辑  收藏  举报