给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。
数据范围:
,矩阵中任意元素都满足
要求:空间复杂度
,时间复杂度 )
#include <stdlib.h> int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) { // write code here int row=matrixRowLen; int col=*matrixColLen; int size=row*col; //*returnSize要及时更新,否则影响外部判断 if (row == 0 || col == 0) { *returnSize = 0; return NULL; } int left = 0, right = col - 1; int top = 0, bottom = row - 1; int direction = 0; // 0: 从左到右, 1: 从上到下, 2: 从右到左, 3: 从下到上 int* result = malloc(size * sizeof(int)); *returnSize = 0; while (left <= right && top <= bottom) { // 0: 从左到右 if (direction == 0) { for (int i = left; i <= right; i++) { result[(*returnSize)++] = matrix[top][i]; } //向下移动 top++; } //1: 从上到下 else if (direction == 1) { for (int i = top; i <= bottom; i++) { result[(*returnSize)++] = matrix[i][right]; } //向左移动 right--; } //2: 从右到左 else if (direction == 2) { for (int i = right; i >= left; i--) { result[(*returnSize)++] = matrix[bottom][i]; } //向上移动 bottom--; } //3: 从下到上 else if (direction == 3) { for (int i = bottom; i >= top; i--) { result[(*returnSize)++] = matrix[i][left]; } //向右移动 left++; } //更新移动方向 direction = (direction + 1) % 4; } return result; }
/** * * @param matrix int整型二维数组 * @param matrixRowLen int matrix数组行数 * @param matrixColLen int* matrix数组列数 * @return int整型一维数组 * @return int* returnSize 返回数组行数 * * C语言声明定义全局变量请加上static,防止重复定义 */ int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) { // write code here if(matrixRowLen == 0) return NULL; int row = matrixRowLen, col = *matrixColLen; int total = row * col; *returnSize = total; int *arr = (int *)malloc(total *sizeof(int)); int cnt = 0,i = 0,j =0; enum {right,down,left,up}pos; pos = right; int circle = 1; //start at circle 1 and right pos; while(cnt < total){ arr[cnt++] = matrix[i][j]; if(pos == right){ //right if(j == col - circle){ pos = down,i++; }else j++; }else if(pos == down){ //down if(i == row - circle){ pos = left,j--; }else i++; }else if(pos == left){ //left if(j == circle - 1){ pos = up,i--; }else j--; }else{ //up if(i == circle){ pos = right,j++,circle++; }else i--; } } return arr; }
int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) { // write code here int i,j,k; int arry[20]; int *p=arry; int m=matrixRowLen; //int n=matrixColLen; int n=(sizeof(matrix))/sizeof(int)*matrixRowLen; //int n=strlen(matrixColLen) int count=0; int top=0; int left=0; int right=n-1; int bootom=m-1; while(count<=m*n) { for(j=left;j<=right;j++) { count++; if(count<=m*n) arry[count-1]=matrix[top][j]; //printf("1:%d\n",matrix[top][j]); else return 0; //printf("count:%d\n",count); //printf("末尾J值:%d\n",j); } for(i=top+1;i<=bootom;i++) { count++; if(count<=m*n) { //printf("开头J值:%d\n",j); arry[count-1]=matrix[i][right]; //printf("2:%d\n",matrix[i][right]); } else return 0; //printf("count:%d\n",count); } for(j=right-1;j>=0;j--) { count++; if(count<=m*n) arry[count-1]=matrix[bootom][j]; //printf("3:%d\n",matrix[bootom][j]); else return 0; //printf("count:%d\n",count); } for(i=bootom-1;i>0;i--) { count++; if(count<=m*n) { //printf("i的值:%d\n",i); //printf("J的值:%d\n",j); arry[count-1]=matrix[i][left]; //printf("4:%d\n",matrix[i][left]); } else return 0; //printf("count:%d\n",count); } ++top; --right; --bootom; ++left; } return arry; }