首页 > 代码库 > Search a 2D Matrix

Search a 2D Matrix

问题:二维数组中是否存在一个数

class Solution {public:    bool dfs(vector<vector<int> > &matrix,int target,int n)    {        if(n==matrix.size()) return false;        if(matrix[n][0]>target) return false;        for(int i=0;i<matrix[n].size();i++)        {            if(matrix[n][i]==target) return true;            if(matrix[n][i]>target) return false;        }        return dfs(matrix,target,n+1);    }    bool searchMatrix(vector<vector<int> > &matrix, int target) {        return dfs(matrix,target,0);    }};