leetcode 搜索二维矩阵 II

注意边界 

class Solution {
public:
    bool searchMatrix(vector>& matrix, int target) {
        int line = matrix.size();
        if(line == 0) return false;
        int bow = matrix[0].size();
        if(bow == 0 ) return false;
        int line_num = 0;
        int bow_num = bow - 1;
        int num = matrix[line_num][bow_num];
        while(line_num < line && bow_num >= 0){
            num = matrix[line_num][bow_num];
            if(num == target){
                return true;
            }
            else if(num > target){
                bow_num --;
            }
            else{
                line_num ++;
            }
        }
        return false;
    }
};

 

你可能感兴趣的:(刷题flag)