嗨,我正在编写用于以螺旋顺序打印 2D 阵列的程序。但我收到以下错误subscripted value is neither array nor pointer nor vector
。
这是我的代码。
int *spiralOrder(const int** A, int n11, int n12, int *length_of_array) {
*length_of_array = n11 * n12; // length of result array
int *result = (int *)malloc(*length_of_array * sizeof(int));
int top = 0, bottom = n11 - 1, left = 0, right = n12 - 1;
int d = 0, j = 0, k = 0;
while (top <= bottom - 1 && left <= right - 1) {
int i;
if (d == 0) { //left to right
for (i = 0; i < right; i++) {
result[j++][k++] = A[top][i];
}
top++;
d = 1;
} else
if (d == 1) { //top to bottom
for (i = 0; i < bottom; i++) {
result[j++][k++] = A[i][right];
}
right--;
d = 2;
} else
if (d == 2) { //bottom right to left
for (i = right; i >= left; i--) {
result[j++][k++] = A[bottom][i];
}
bottom--;
d = 3;
} else
if (d == 3) { //bottom left to top
for (i = bottom; i >= top; i--) {
result[j++][k++] = A[i][left];
}
left++;
d = 0;
}
}
return result;
}
我想将结果保存在result
数组中。我看到了这些错误的答案,但其中大多数都在处理一维数组。有人可以帮忙吗。