Concise Java solution


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

Log in to reply
 

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