1.题目要求如图所示:
各位看官们,大家好呀,今天小编用的方法比较麻烦,就是按顺时针遍历,但也挺好理解的,因为就是迭代法循环,所以就不给大家讲步骤了,直接就发代码了:
/*** Note: The returned array must be malloced, assume caller calls free().*/
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {int col = *matrixColSize;int total = col * matrixSize;int* number = (int*)malloc(sizeof(int) * total);int i = 0;int j = 0;int f = 0;while(f < total){int i_t = i;int j_t = j;while(j_t < col - j){if(f >= total)break;number[f] = matrix[i_t][j_t];f++;j_t++;}if(f >= total){break;}j_t--;i_t++;while(i_t < matrixSize - i){if(f >= total)break;number[f] = matrix[i_t][j_t];f++;i_t++;}if(f >= total){break;}i_t--;j_t--;while(j_t >= j){if(f >= total)break;number[f] = matrix[i_t][j_t];f++;j_t--;}if(f >= total){break;}j_t++;i_t--;while(i_t > i){if(f >= total)break;number[f] = matrix[i_t][j_t];f++;i_t--;}if(f >= total){break;}i++;j++;}*returnSize = total;return number;
}
大家如果觉得好的话,就请给个免费的赞吧,谢谢了^ _ ^