代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷



描述可能有些不正确,但我无法弄清楚我的代码出了什么问题。

我尝试解决CodeSignal中的一个问题。我写下了以下代码片段,但是当它运行时,我看到以下结果。

std::vector<std::vector<int>> rotateImage(std::vector<std::vector<int>> a) {
int hsize = a.size() / 2;
int row = 0;
for (; row < hsize; ++row) {
std::vector<int>& row_top = a[row];
std::vector<int>& row_bottom = a[a.size() - row - 1];
int row_width = a[row].size();
for (int col = 0; col < row_width / 2; ++col) {
int& cell_top_left = row_top[col];
int& cell_top_right = row_top[row_top.size() - col - 1];
int& cell_bottom_left = row_bottom[col];
int& cell_bottom_right = row_bottom[row_bottom.size() - col - 1];
// rotateCellsCW(cell_top_left, cell_top_right, cell_bottom_left, cell_bottom_right);
}
cout << "LOOP: " << row << " " << hsize << endl;
}
}
╔═════════════════╦═══════════╗
║      Field      ║   Value   ║
╠═════════════════╬═══════════╣
║ Input           ║ a:        ║
║                 ║ [[1,2,3], ║
║                 ║  [4,5,6], ║
║                 ║  [7,8,9]] ║
║ Output          ║ undefined ║
║ Expected Output ║ [[7,4,1], ║
║                 ║  [8,5,2], ║
║                 ║  [9,6,3]] ║
║ Console Output  ║ LOOP: 0 1 ║
║                 ║ LOOP: 1 1 ║
║                 ║ LOOP: 2 1 ║
║                 ║ LOOP: 3 1 ║
║                 ║ LOOP: 4 1 ║
║                 ║ LOOP: 5 1 ║
║                 ║ LOOP: ... ║
╚═════════════════╩═══════════╝

我不确定为什么会发生这种情况,但在我看来,这是一个缺陷。

经过几个小时试图找出问题所在,我终于发现,它需要从函数返回才能使循环停止运行。我不认为这是否合乎逻辑,但无论如何。这就是解决问题的办法。

相关内容

最新更新