コード例 #1
0
ファイル: search2DMatrix.II.cpp プロジェクト: lw9210/letcode
    bool searchMatrix03(vector<vector<int> > &matrix, int target) {
        int m = matrix.size();
        int n = m > 0 ? matrix[0].size() : 0;
        if (m == 0 || n == 0) return false;

        for (int i = 0; i < m; i++) {
            return binary_search_column(matrix[i], target);
        }
        return false;
    }
コード例 #2
0
ファイル: YoungTableau.c プロジェクト: BonusTeam/CodeHome
void findElemFromMatrix(int matrix[][4], int row, int column, int elem)
{
    if (matrix == NULL || row <= 0 || column <= 0)
            return ;

    int top_right_row = 0;
    int top_right_col = column - 1;

    // Socialite Problem
    // Check the top right corner element matrix[i][j]
    //       if matrix[i][j] > elem, delete the whole j column,
    //       otherwise, delete the whole i row.
    while ((top_right_row <= (row - 1)) && (top_right_col >= 0)){
        if (matrix[top_right_row][top_right_col] == elem ){
            return output_result(elem, top_right_row, top_right_col);
        }else if (matrix[top_right_row][top_right_col] < elem){
            ++top_right_row;
        }else{
            --top_right_col;
        }
    }

    // In case the last row,
    // Binary search to find the elem
    if (top_right_row == row - 1){
        return binary_search_row(matrix, row - 1, top_right_col, elem);
    }

    // In case the last column
    // Binary search to find the elem
    if (top_right_col == 0){
        return binary_search_column(matrix, top_right_row, row - 1, elem);
    }

    cout << "The elem " << elem << " is not in this matrix" <<endl;
    return ;
}