240. Search a 2D Matrix II


June-21-2019

这个题居然也没记录过?

没法直接二分,因为不是完全排列的。

左下开始

O(row + col)

    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
        
        int row = matrix.length - 1;
        int col = 0;
        while (row >= 0 && col < matrix[0].length) {
            int tempVal = matrix[row][col];
            if (tempVal == target) {
                return true;
            } else if (tempVal > target) {
                row --;
            } else {
                col ++;
            }
        }
        
        return false;
    }
posted @ 2019-06-22 11:09  哇呀呀..生气啦~  阅读(107)  评论(0编辑  收藏  举报