My java solution using binary search


  • 0
    M
    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            int m=matrix.length;
            int n=matrix[0].length;
            if(m==0)
            return false;
            int i=m-1,j=0;
            while(i>=0 && j<n)
            {
                if(matrix[i][j]==target)
                return true;
                else if(matrix[i][j]<target)
                {
                    j++;
                }
                else
                {
                    i--;
                }
            }
            return false;
        }
    }

  • 6
    Y

    I think this is O(m + n) solution rather than Binary Search.


  • 0
    S

    How is this binary search?


Log in to reply
 

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