54. Spiral Matrix
来源:互联网 发布:mac自带远程桌面连接 编辑:程序博客网 时间:2024/06/05 19:43
螺旋式遍历矩阵
如下图所示的方法。
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].
思路
递归实现
code
public class Solution { private IList<int> rtn = new List<int>(); public IList<int> SpiralOrder(int[,] matrix) { int m = matrix.GetUpperBound(0); //row index upper bound int n = matrix.GetUpperBound(1); // spiral(matrix, 0, n, m, 0); return rtn; } public void spiral(int[,] matrix, int top, int right, int bottom, int left) { if (top > bottom || left > right) return; //add first row for (int j = left; j <= right; j++) rtn.Add(matrix[top, j]); //add last column for (int i = top + 1; i <= bottom; i++) rtn.Add(matrix[i, right]); //add last row for (int j = right - 1; j >= left && top!=bottom; j--) rtn.Add(matrix[bottom, j]); //add first column for (int i = bottom - 1; i > top && left!=right; i--) rtn.Add(matrix[i, left]); spiral(matrix, top + 1, right - 1, bottom - 1, left + 1); } }
阅读全文
2 0
- 54. Spiral Matrix && 59. Spiral Matrix II
- 59. Spiral Matrix II && 54. Spiral Matrix
- LeetCode 54. Spiral Matrix
- [LeetCode]54.Spiral Matrix
- LeetCode --- 54. Spiral Matrix
- [Leetcode] 54. Spiral Matrix
- [leetcode] 54.Spiral Matrix
- 54. Spiral Matrix
- Leetcode-54.Spiral Matrix
- **LeetCode 54. Spiral Matrix
- Leetcode 54. Spiral Matrix
- [leetcode] 54. Spiral Matrix
- 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- 54. Spiral Matrix LeetCode
- 54. Spiral Matrix
- 54. Spiral Matrix
- leetcode 54. Spiral Matrix
- C
- 学习笔记——object detection+DL的发展(RCNN→Fast RCNN→Faster RCNN)
- Linux启动Tomcat返回JRE_HOME字段错误
- 数据挖掘系列(四)聚类算法评价指标
- 解除认证攻击
- 54. Spiral Matrix
- SpringMVC 拦截器实现
- 文章标题
- Stream 里面的ifPresent用法
- Linux 文件特殊权限
- 基于pycaffe从零开始写mnist(第四篇)——生成train_loss图片
- Python初入门(六)(Head First Python 第六章 打包代码与数据 ,字典,类与继承)
- 类加载器、双亲委派模型
- jQuery的扩展与noConflict小结