Leetcode刷题笔记(c++)_热题 HOT 100_240. 搜索二维矩阵 II

递归

class Solution {
public:
    int row=0,col=0;
    bool dfs(vector<vector<int>>& matrix, int target,int i,int j){
        // cout<
        if(matrix[i][j]==target)return 1;
        else if(matrix[i][j]>target&&j-1>=0){
            if(dfs(matrix,target,i,j-1))return 1;
        }else if(matrix[i][j]<target&&i+1<row){
            if(dfs(matrix,target,i+1,j))return 1;
        }
        return 0;
    }
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        row=matrix.size();
        col=matrix[0].size();
        return dfs(matrix,target,0,col-1);
    }
};

Leetcode刷题笔记(c++)_热题 HOT 100_240. 搜索二维矩阵 II_第1张图片

你可能感兴趣的:(leetcode,c++,矩阵)