54. Spiral Matrix

问题描述

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]
简单讲,就是将一个 m*n的二维数组,螺旋的方式输出;

解法一

递归将问题化解为子问题,以(rt,ct), (re,ce) 为左上和右下点的矩阵螺旋输入到vector尾部;使用递归;需要考虑边界条件(row ==1 columns ==1);
参考代码
运行情况

runtime

算法难度不大,主要是实现细节需要考虑;

解法二:

使用循环,而非递归方式; 略

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容