Can anyone help me figure why I keep getting runtime error?


  • 0
    W
    public boolean searchMatrix(int[][] matrix, int target) {
        
        int row = matrix.length;
        int col = matrix[0].length;
        int i = 0;
        int j = row * col-1;
        
        int mid = i+(i-j)/2;
        
        while(i <= j){
            int mid_val = matrix[mid/col][mid%col];
            if(target == mid_val) return true;
            if(target < mid_val){
                j = mid-1;
            }else{
                i = mid+1;
            }
            mid = i+(i-j)/2;
        }
        return false;
        
    }
    

    I think i wrote pretty clear and really dont know why run time error thnks


  • 0
    D

    You flipped i & j in calculation of "mid". So it should be mid = i + (j - i) / 2;


Log in to reply
 

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