clear and easy solution


  • 0
    C
    public class Solution {
        public boolean searchMatrix(int[][] matrix, int target) {
            if(matrix.length==0 || matrix[0].length==0){
                return false;
            }
            int m = matrix.length-1;
            int n = matrix[0].length - 1;
            int row = 0;
            while(row<=m){
                if(target<matrix[row][n]){
                    return search(matrix,row,target);
                }else if(target>matrix[row][n]){
                    row++;
                }else{
                    return true;
                }
            }
            return false;
        }
        
        public boolean search(int[][] matrix, int row, int target){
            int l = 0;
            int h = matrix[0].length-1;
            while(l<=h){
                int mid = l + (h-l)/2;
                if(target==matrix[row][mid]){
                    return true;
                }else if(target>matrix[row][mid]){
                    l = mid + 1;
                }else{
                    h = mid - 1;
                }
            }
            return false;
        }
    }
    

Log in to reply
 

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