54. Spiral Matrix
来源:互联网 发布:vb中名称和caption 编辑:程序博客网 时间:2024/05/22 12:05
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]
.
旋转打印矩阵。思路如下:1、先从左往右打印(没有条件限制);2、打印矩阵的右边界(需满足矩阵的列数大于1,否则会有1重叠);3、打印矩阵的下边界(需满足矩阵的行数大于1,否则会与1重叠);3、答应矩阵的左边界(需满足列数大于1,否则与1重叠)。
程序如下:
class Solution { public List<Integer> spiralOrder(int[][] matrix) { int rowMin = 0, rowMax = matrix.length, colMin = 0; if (matrix == null||rowMax == 0){ return new ArrayList<Integer>(); } int colMax = matrix[0].length; List<Integer> lst = new ArrayList<>(); if (colMax == 0){ return lst; } while (rowMin < rowMax&&colMin < colMax){ for (int i = colMin; i < colMax; ++ i){ lst.add(matrix[rowMin][i]); } for (int i = rowMin + 1; colMax >= 1&&i < rowMax; ++ i){ lst.add(matrix[i][colMax-1]); } for (int i = colMax - 2; rowMax - rowMin > 1&&i >= colMin; -- i){ lst.add(matrix[rowMax-1][i]); } for (int i = rowMax - 2; colMax - colMin > 1&&i > rowMin; -- i){ lst.add(matrix[i][colMin]); } rowMin ++; rowMax --; colMin ++; colMax --; } return lst; }}
阅读全文
0 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
- 【CGROUP】10分钟深入理解cgroup与css之间的多对多的关系
- 1042. 字符统计(20)
- java.lang.OutOfMemoryError: GC overhead limit exceeded 问题分析和解决
- css实现基本的鼠标滑过图片添加遮蔽层效果
- 1043. 输出PATest(20)
- 54. Spiral Matrix
- Sql Server with(nolock)的使用
- 实现一个函数,打印出乘法口诀
- VC6LineNumberAddin.dll 在win7x64下的破解
- Python中的JSON模块详解
- 深度学习之数学基础(概率与统计)
- Koch雪花曲线的MATLAB实现
- Django 博客
- 2017.10.26日报