Two binary-search solution in C++


  • 0
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int left = 0, right = matrix.size(), mid;
        while( left < right ) {
            mid = (left + right)/2;
            if( target < matrix[mid].front() ) {
                right = mid;
            } else if( target > matrix[mid].back()) {
                left = mid+1;
            } else break;
        }
        return *(lower_bound(matrix[mid].begin(), matrix[mid].end(), target )) == target;
    }

Log in to reply
 

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