Binary Search in Medium level?


  • 0
    N

    I don't understand why BS is medium level, but...

    bool searchMatrix(vector<vector<int> > &matrix, int target)
    {
        int n = matrix[0].size();
        int low = 0, high = matrix.size()*n -1;
        
        while(low <= high)
        {
            int mid =(high+low)/2;
            if(matrix[mid/n][mid%n] < target) low = mid+1;
            else if(matrix[mid/n][mid%n] > target) high = mid-1;
            else return true;
        }
        
        return false;
    }

Log in to reply
 

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