9ms c++ solution

  • 0
    class Solution {
        bool binarySearch(vector<int>m, int t, int s, int e)
            int mid = (s+e)/2;
            if (e<s)
                return false;
            else if (m[mid]==t)
                return true;
            else if (m[mid]<t)
                return binarySearch(m,t,mid+1,e);
                return binarySearch(m,t,s,mid-1);
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            for(int i=0;i<matrix.size();i++)
                if (target>=matrix[i][0]&&target<=matrix[i][matrix[i].size()-1])
                    return binarySearch(matrix[i], target, 0, matrix[i].size()-1);
            return false;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.