public static boolean searchMatrix(ArrayList<ArrayList<Integer>> matrix, int target) {
        // write your code
    	if(matrix == null || matrix.size() == 0)
    		return false;
    	
    	int rowL = matrix.size();
    	int colL = matrix.get(0).size();
    	int start = 0, end = rowL*colL;
    	while(start <= end){  
    		int mid = (start + end)/2;
    		
    		int rowIndex = mid/colL;
    		int colIndex = mid%colL;
    		
    		int val = Integer.MAX_VALUE;
    		
    		//!!!here we need to pay much attention. 
    		// the rowIndex or colIndex might out of bound if the target is larger than the 
    		// largest element in the matrix
    		// so here, make sure rowIndex and colIndex are in bound
    		if(rowIndex >=0 && rowIndex < rowL && colIndex >=0 && colIndex < colL){
    			val = matrix.get(rowIndex).get(colIndex);
    		}
    		if(target == val)
    			return true;
    		else if(target < val){
    			//
    			end = mid-1 ;
    		}else{
    			start = mid+1 ;
    		}
    	}
    	return false;
    }