/** * * @param matrix int整型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) { // write code here if (matrix == NULL) return NULL; int *arr = (int*)malloc(sizeof(int) * (matrixRowLen * (*matrixColLen))); //存放返回的数组 int inRow = 0; int inCol = 0; int i = 0; int finRow = matrixRowLen - 1; int finCol = (*matrixColLen) - 1; if (*matrixColLen == 1 && matrixRowLen == 1) { arr[0] = matrix[0][0]; } else { while (i <= (matrixRowLen * (*matrixColLen) - 1)) { if (i <= (matrixRowLen * (*matrixColLen) - 1)) for (int k = inCol; k <= finCol; k++)//上面一行 { arr[i++] = matrix[inRow][k]; } inRow += 1; if (i <= (matrixRowLen * (*matrixColLen) - 1)) for (int j = inRow; j <= finRow; j++)//右边一列 { arr[i++] = matrix[j][finCol]; } finCol -= 1; if (i <= (matrixRowLen * (*matrixColLen) - 1)) for (int k = finCol; k >= inCol; k--)//下面一行 { arr[i++] = matrix[finRow][k]; printf("%d ",k); } finRow -= 1; if (i <= (matrixRowLen * (*matrixColLen) - 1)) for (int j = finRow; j >= inRow; j--)//左边一列 { arr[i++] = matrix[j][inCol]; } inCol += 1; } } *returnSize = matrixRowLen * (*matrixColLen); return arr; }