首页 > 试题广场 >

螺旋矩阵

[编程题]螺旋矩阵
  • 热度指数:139185 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。

数据范围:,矩阵中任意元素都满足
要求:空间复杂度 ,时间复杂度
示例1

输入

[[1,2,3],[4,5,6],[7,8,9]]

输出

[1,2,3,6,9,8,7,4,5]
示例2

输入

[]

输出

[]
#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;
}

发表于 2023-09-16 15:26:19 回复(0)

/**
 *
 * @param matrix int整型二维数组
 * @param matrixRowLen int matrix数组行数
 * @param matrixColLen int* matrix数组列数
 * @return int整型一维数组
 * @return int* returnSize 返回数组行数
 */
 #include <time.h>
#include <string.h>

/*想到递归指关键*/

struct localStation
{
    int x0;
    int x1;
    int y0;
    int y1;
}localStation;

void circle(int *pt,int *ptCounter, int **matrix, struct localStation *ptLocal)
{
    if(ptLocal->x1 == ptLocal->x0)
    {
        for(int j =ptLocal->y0; j <=ptLocal->y1;j++)
        {
            *pt =matrix[ptLocal->x0][j];
             pt++;
            (*ptCounter)++;
        }
        return;
    }
    if(ptLocal->y1 == ptLocal->y0)
    {
        for(int i = ptLocal->x0; i <=ptLocal->x1;i++)
        {
            *pt =matrix[i][ptLocal->y0];
             pt++;
            (*ptCounter)++;
        }
        return;
    }
    for(int j = ptLocal->y0; j <= ptLocal->y1 ;j++ )/*Right*/
    {
        *pt =matrix[ptLocal->x0][j];
        pt++;
       (*ptCounter)++;
    }
    for(int i = ptLocal->x0+1; i <=ptLocal->x1; i++)/*Down*/
    {
        *pt =matrix[i][ptLocal->y1];
         pt++;
       (*ptCounter)++;

    }
    for(int j = ptLocal->y1-1; j>= ptLocal->y0;j--) /*Left*/
    {
        *pt =matrix[ptLocal->x1][j];
         pt++;
       (*ptCounter)++;
    }
    for(int i = ptLocal->x1-1; i >=ptLocal->x0+1;i--)/*UP*/
    {
        *pt = matrix[i][ptLocal->y0];
        pt++;
        (*ptCounter)++;
    }

    ptLocal->x0 ++;
    ptLocal->x1 --;
    ptLocal->y0++;
    ptLocal->y1--;
    if(ptLocal->x1 >= ptLocal->x0 && ptLocal->y1 >=ptLocal->y0 )
    {
         circle(pt,ptCounter, matrix, ptLocal);
    }
   
    return;
}
int* spiralOrder(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) {
    if(matrixColLen == 0 || * matrixColLen == 0)
    {
        return NULL;
    }
   
    int *pt = (int *)malloc(sizeof(int)*matrixRowLen * (*matrixColLen));
    memset(pt, 0, sizeof(int)*matrixRowLen * (*matrixColLen));
    int *ptFirst = pt;
    int iCounter = 0;
   
    struct localStation tLocalStation;
    memset(&tLocalStation, 0, sizeof(tLocalStation));
    tLocalStation.x0 = 0;
    tLocalStation.x1 = matrixRowLen-1;
    tLocalStation.y0 = 0;
    tLocalStation.y1 = *matrixColLen-1;
    circle(pt,&iCounter, matrix, &tLocalStation);
    *returnSize = iCounter;
    return ptFirst;
}

发表于 2023-02-19 16:35:35 回复(0)
为什么要用int *a=(int*)malloc(sizeof(int*)*k);来分配一个动态空间。
而用int a[k] 不行,值都是垃圾值
发表于 2022-09-12 19:26:06 回复(1)
#include <stdio.h>
void SpiralMatrix(int row, int column,int arr[3][3])
{
    int i = 0; int j = 0; int count = 0;
    while (row!=1)
    {
        while (j < column)
        {
            printf("%d ", arr[i][j]);
            j++;
        }
        while(i < row)
        {
            printf("%d ", arr[i][j]);
            i++;
        }
        while (j > 0)
        {
            printf("%d ", arr[i][j]);
            j--;
        }
        while (i > 0)
        {
            printf("%d ", arr[i][j]);
            i--;
        }
        count++;
        column--;
        row--;
        i = count;
        j = count;
    }

}
int main()
{
    int arr[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
     SpiralMatrix(2,2,arr);
}


发表于 2022-01-16 17:03:46 回复(0)
/**
 * 
 * @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;
}

发表于 2021-12-25 17:24:36 回复(0)
//为什么最后返回的一位数组一定要用calloc出来的才能正确提交啊,我创建一个int一维数组然后int *p指向它,最后返回p不行吗😔
发表于 2021-12-10 13:51:00 回复(2)
#include<stdio.h>
#include<string.h>
void main()
{
    int m=3,n=3,i,j,k,t;
    char a_in[11][128]={"123","456","789"};
    char a_out[128]={0};
//    scanf("%d%d",&m,&n);
    t=m*n;
    for(i=0;i<n;i++)
    {
        a_out[i]=a_in[0][i];

    }
    for(j=1;j<=(m-2);j++)
    {
        a_out[i+j-1]=a_in[j][n-1];
//        a_out[3]=a_in[1][2];
    }
    for(k=0;k<n;k++ )
    {
        a_out[i+j+k-1]=a_in[m-1][n-k-1];
    }
    a_out[i+j+k-1]='4';
    a_out[i+j+k]='5';
    printf("%d%d\n",m,n);
    printf("%s",a_out);
    while(1);
}
发表于 2021-11-28 17:46:59 回复(0)
int main(void)
{
    /*int a[2],i;
    int* p = a;*/
    int matrix[3][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
    //int* a1 = *num;
    //int **matrix = &a1;

    int N=3;
    int b[12];
    int* p;

        int n = 4;
        int i, j;
        int js = 0;

        for (i = 0; i <= N / 2; i++)
        {
            for (j = i; j <= n - i - 1; j++)
            {
                b[js++] = *(*(matrix +i) +  j);
            }
            for (j = i + 1; j <= N - i - 1; j++)
            {
                b[js++] = *(*( matrix +j) + n - i - 1);
            }
            for (j = n - i - 2; j >= i; j--)
            {
                b[js++] = *(*(matrix +  N - i - 1) +j);
            }
            for (j = N - i - 2; j>i; j--)
            {
                b[js++] = *(*(matrix + j) + i);
            }
        }


    for (int i = 0; i<12; i++)
            {
                printf("%5d", b[i]);
            }
    return 0;
}
发表于 2021-09-12 10:42:22 回复(0)
请指教
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;
}

发表于 2021-09-05 19:32:35 回复(0)

问题信息

难度:
9条回答 24307浏览

热门推荐

通过挑战的用户