A simple solution based on binary search


  • 0
    C
    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if (matrix==null || matrix.length==0) return false;
            
            int rows = matrix.length, cols = matrix[0].length;
            int start = 0, end = rows * cols;
            while (start < end) {
                int mid = start + (end - start) / 2;
                if (matrix[mid/cols][mid%cols] == target) {
                    return true;
                } else if (matrix[mid/cols][mid%cols] < target) {
                    start = mid + 1;
                } else {
                    end = mid;
                }
            }
            
            return false;
        }
    }

Log in to reply
 

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