Test case may be too weak !!!


  • 3
    E

    The naive O(mn) method passed in 1 ms...

    public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        if(m<1) return false;
        int n = matrix[0].length;
        
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++)
                if(matrix[i][j]==target) return true;
        }
        return false;
    }
    }
    

    or even more simplified one following stephen's suggestion (I hate myself forgetting foreach all the time):

    public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        for(int[] row : matrix) {
            for(int item : row)
                if(item==target) return true;
        }
        return false;
    }
    }

  • 1
    Y

    yes, but you have to give the best solution during the interview. :)


Log in to reply
 

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