My java solution using binary search


  • 0
    S
    public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        
        int m = matrix.length;
        if(m==0) return false;
        int n = matrix[0].length;
        int start=0;
        int end=m*n-1;
        while(start<=end)
        {
            int mid=start+(end-start)/2;
            int row=mid/n;
            int col=mid%n;
            if(matrix[row][col]==target)
            {
                return true;
            }
            else if (matrix[row][col]>target)
            {
                end=mid-1;
            }
            else
            {
                start=mid+1;
            }
        }
        
        return false;
    }
    

    }


Log in to reply
 

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