74. Search a 2D Matrix && 240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

 

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

 

For example,

Consider the following matrix:

[
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]

Given target = 3, return true.

 

Hide Similar Problems
 (M) Search a 2D Matrix II
 
 
public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row_num = matrix.length;
        int col_num = matrix[0].length;
        //Treat it as a one-dimension array.
        int begin = 0, end = row_num * col_num - 1;
        while(begin <= end){
            int mid = (begin + end) / 2;
            int mid_value = matrix[mid/col_num][mid%col_num];
    
            if( mid_value == target){
                return true;
            }else if(mid_value < target){
                begin = mid+1;//Otherwise infinite loop.
            }else{
                end = mid-1;
            }
        }
        return false;
    }
}

 

240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

 

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

 

For example,

Consider the following matrix:

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

 

O(M+N) solution:

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix == null || matrix.length < 1 || matrix[0].length <1) {
            return false;
        }
        int col = matrix[0].length-1;
        int row = 0;
        while(col >= 0 && row <= matrix.length-1) {
            if(target == matrix[row][col]) {
                return true;
            } else if(target < matrix[row][col]) {
                col--;
            } else if(target > matrix[row][col]) {
                row++;
            }
        }
        return false;
    }
}

 

2D Binary Search. Divide and Conquer (log4/3(M*N)).

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        return searchMatrix(matrix, target, 0, matrix.length-1, 0, matrix[0].length-1);
    }
    
    //[rFrom, rTo], [cFrom, cTo]
    private boolean searchMatrix(int[][] matrix, int target, int rFrom, int rTo, int cFrom, int cTo) {
        if(rFrom > rTo || cFrom > cTo)
            return false;
        if(rFrom == rTo && cFrom == cTo)
        {
            if(matrix[rFrom][cFrom] == target)
                return true;
            return false;
        }
        
        int rMid = (rFrom+rTo)/2;
        int cMid = (cFrom+cTo)/2;
        if(matrix[rMid][cMid] == target)
            return true;
        
        if(matrix[rMid][cMid] < target)
            return 
            searchMatrix(matrix, target, rFrom, rTo, cMid+1, cTo) ||   // right side region
            searchMatrix(matrix, target, rMid+1, rTo, cFrom, cMid);   //lower left region
        return 
            searchMatrix(matrix, target, rFrom, rMid-1, cFrom, cTo) ||  //up side region
            searchMatrix(matrix, target, rMid, rTo, cFrom, cMid-1);   //lower left region
            
    }
}

 

M*log2(N): Do the 1D binary search for each row.

 

posted @ 2016-06-23 14:32  新一代的天皇巨星  阅读(191)  评论(0)    收藏  举报