leetcode-54. Spiral Matrix(打印蛇形矩阵)
来源:互联网 发布:720全景图制作软件 编辑:程序博客网 时间:2024/06/02 03:32
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].
代码如下:
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result=new ArrayList<>(); if(matrix.length==0)return result; int rs=0; int cs=0; int re=matrix.length-1; int ce=matrix[0].length-1; while(rs<=re && cs<=ce) { for(int i=cs;i<=ce;++i) { result.add(matrix[rs][i]); } rs++; for(int i=rs;i<=re;++i) { result.add(matrix[i][ce]); } ce--; if(rs<=re) { for(int i=ce;i>=cs;i--) { result.add(matrix[re][i]); } } re--; if(cs<=ce) { for(int i=re;i>=rs;i--) { result.add(matrix[i][cs]); } } cs++; } return result; }}
阅读全文
0 0
- leetcode-54. Spiral Matrix(打印蛇形矩阵)
- leetcode54-Spiral Matrix(打印蛇形矩阵)
- LeetCode | Spiral Matrix(顺时针打印矩阵)
- leetcode-59. Spiral Matrix II(打印蛇形方阵)
- LeetCode:蛇形矩阵II(spiral matrix II)
- LeetCode-54. Spiral Matrix (JAVA)(顺时针打印矩阵)
- Array-----54. Spiral Matrix(蛇形填矩阵)
- Array-----59. Spiral Matrix II(蛇形打印矩阵)
- LeetCode 54. Spiral Matrix(螺旋矩阵)
- Leetcode 54 - Spiral Matrix(螺旋打印矩阵)
- [LeetCode] Spiral Matrix 旋转打印矩阵
- LeetCode------54. Spiral Matrix(螺旋二维矩阵)-----59. Spiral Matrix II(n的螺旋矩阵)
- leetcode59-Spiral Matrix II(生成蛇形矩阵)
- leetcode 59 Spiral Matrix II 螺旋矩阵的打印
- Spiral Matrix:旋转打印矩阵
- leetcode-54 Spiral Matrix 顺时针打印矩阵(《剑指offer》面试题20)
- LeetCode Spiral Matrix(螺旋矩阵)
- LeetCode 54. Spiral Matrix
- Spring配置多数据源
- 第二周周总结
- ViewPager
- PDCCH时域资源
- 安装codis1.9版本全程
- leetcode-54. Spiral Matrix(打印蛇形矩阵)
- Swift--06数组字典
- 【腾讯Bugly干货分享】Android 插件技术实战总结
- 青蛙的约会(扩展欧几里得解同余方程)
- QT:标准对话框
- leetcode 3sum问题
- rxjava,okhttp 合并请求
- DAG上的动态规划硬币问题
- 软件设计目标—可维护性—基于面向对象技术的计算机程序