1

问题如图所示

vector<int> printMatrix(vector<vector<int> > matrix) {
    if (matrix.empty()) {
        return vector<int>();
    }
    this->matrix = std::move(matrix);
    int startRow = 0, lastRow = this->matrix.size() - 1;
    int startCol = 0, lastCol = this->matrix[0].size() - 1;
    while (startRow <= lastRow && startCol <= lastCol) {
        printCircle(startCol, lastCol, startRow, lastRow);
        ++startCol, --lastCol, ++startRow, --lastRow;
    }
}

它工作正常,而变量 startRow 小于 lastRow 。然而,一般来说,当 startRow 大于 lastRow 时,应该退出 while 循环但引发 Exception: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 代替。如图所示,我对引发异常感到困惑。

4

1 回答 1

4
vector<int> printMatrix(vector<vector<int> > matrix) {
    if (matrix.empty()) {
        return vector<int>();
    }
    this->matrix = std::move(matrix);
    int startRow = 0, lastRow = this->matrix.size() - 1;
    int startCol = 0, lastCol = this->matrix[0].size() - 1;
    while (startRow <= lastRow && startCol <= lastCol) {
        printCircle(startCol, lastCol, startRow, lastRow);
        ++startCol, --lastCol, ++startRow, --lastRow;
    }
    // **** return a vector here ****
}

需要从函数中返回一个向量,或者将其更改为 void。

于 2018-01-23T15:20:00.807 回答