Simple java 1 ms solution


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

Log in to reply
 

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