[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 in spir...

LeetCode-54.Spiral Matrix

一、问题描述 给定一个m*n的矩阵,返回从[0,0]开始的矩阵螺旋列表。 例如给定矩阵[ [1,2,3],[4,5,6],[7,8,9] ],返回列表内容为[1,2,3,6,9,8,7,4,5]. 二...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

[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

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

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

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

问题描述https://leetcode.com/problems/spiral-matrix/#/description Given a matrix of m x n elements (m r...

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

Problem:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in s...

leetcode - 54. Spiral Matrix

54. Spiral Matrix Given a matrix of m x n elements(m rows, n columns), return all elements of thema...
  • laeen
  • laeen
  • 2017-03-02 21:08
  • 64
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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