Leetcode 54. Spiral Matrix (Medium) (java)
来源:互联网 发布:mac中我的所有文件 编辑:程序博客网 时间:2024/05/16 09:17
Leetcode 54. Spiral Matrix (Medium) (java)
Tag: Array
Difficulty: Medium
/*54. Spiral Matrix (Medium)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> res = new ArrayList<>(); if (matrix == null || matrix.length == 0) { return res; } int m = matrix.length - 1, n = matrix[0].length - 1, h = 0, l = 0; while (true) { for (int col = l; col <= n; col++) { res.add(matrix[h][col]); } if (++h > m) { break; } for (int row = h; row <= m; row++) { res.add(matrix[row][n]); } if (l > --n) { break; } for (int col = n; col >= l; col--) { res.add(matrix[m][col]); } if (--m < h) { break; } for (int row = m; row >= h; row--) { res.add(matrix[row][l]); } if (++l > n) { break; } } return res; }}
0 0
- Leetcode 54. Spiral Matrix (Medium) (java)
- LeetCode 54.Spiral Matrix (Medium)
- Leetcode 59. Spiral Matrix II (Medium) (java)
- Leetcode 54. Spiral Matrix (Medium) (cpp)
- [Leetcode 54, Medium] Spiral Matrix
- 【LeetCode】(54)Spiral Matrix(Medium)
- LeetCode 59.Spiral Matrix || (Medium)
- leetcode-java-54. Spiral Matrix
- (Java)LeetCode-54. Spiral Matrix
- [leetcode]54. Spiral Matrix(Java)
- 【LeetCode-Java】54. Spiral Matrix+59. Spiral Matrix II
- 【LeetCode】(59)Spiral Matrix II(Medium)
- LeetCode-59-Spiral Matrix II(Array)-Medium
- Leetcode 59. Spiral Matrix II (Medium) (cpp)
- [Leetcode] Spiral Matrix (Java)
- [LeetCode][Java] Spiral Matrix
- leetcode:Spiral Matrix 【Java】
- Leetcode oj java 54. Spiral Matrix
- 简单搭建iOS开发项目框架
- 高德地图-定位
- Windows下安装PHP7提示丢失VCRUNTIME140.DLL
- C++: 背包问题
- vpn connecting to client1 has failed
- Leetcode 54. Spiral Matrix (Medium) (java)
- Faster R-CNN 英文论文翻译笔记
- HDU 5546 Ancient Go (dfs)
- swift--Swift 3.0实战:从使用场景了解GCD新API
- 动态代理模式
- JDK1.8新特性
- 使用切片(Slicing)准备按钮模板
- Android 编码规范
- android真机测试时候的遇到问题