[leetcode-54]Spiral Matrix(C语言)

原创 2015年07月08日 20:52:50

问题描述:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].

这道题没啥可说的,上代码:

int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
        int length = matrixRowSize*matrixColSize;
    int *result = (int *)malloc(sizeof(int)*length);
    char *visited = (char *)calloc(length,sizeof(char));//store visited element

    int index = 0;
    int row=0,col=-1;
    while(1){
        if(index == length)
            break;
        for(col=col+1;col<matrixColSize;col++){
            if(visited[row*matrixColSize+col] == 1)         
                break;
            result[index++]=matrix[row][col];
            visited[row*matrixColSize+col] = 1;
        }
        col--;
        for(row = row+1;row<matrixRowSize;row++){
            if(visited[row*matrixColSize+col] == 1)
                break;
            result[index++] = matrix[row][col];
            visited[row*matrixColSize+col] = 1;
        }
        row--;
        for(col = col-1;col>=0;col--){
            if(visited[row*matrixColSize+col] == 1)
                break;
            result[index++] = matrix[row][col];
            visited[row*matrixColSize+col] = 1;
        }
        col++;
        for(row = row-1;row>=0;row--){
            if(visited[row*matrixColSize+col] == 1)         
                break;
            result[index++] = matrix[row][col];
            visited[row*matrixColSize+col] = 1;
        }
        row++;
    }
    return result;
}

相关文章推荐

【leetcode】【54】Spiral Matrix

一、问题描述 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

leetcode-java-54. Spiral Matrix

/* Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral...

(java)leetcode-54:Spiral Matrix

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma...

LeetCode#54. Spiral Matrix

按照Spider Order遍历数组:一个二维数组瞬时间绕圈走,将经过的数组元素以List返回...

[LeetCode]54.Spiral Matrix

【题目】 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in...

Leetcode 54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or...

LeetCode54/59 Spiral Matrix I/II

一:Spiral Matrix I 题目: Given a matrix of m x n elements (m rows, n columns), return all elements ...

[LeetCode]--54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or...

leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of th...

LeetCode - 54. Spiral Matrix

这道题目也需要寻找矩阵中下标的一定规律,题目中给出的例子是n = 3, m = 3的情况,所以可以先下出来下标的变化情况然后寻找一定的规律。 在逐渐寻找规律的时候,可以发现每次都是将外面一层的元素放...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode-54]Spiral Matrix(C语言)
举报原因:
原因补充:

(最多只允许输入30个字)