leetcode 54. Spiral Matrix
来源:互联网 发布:软件直销网 编辑:程序博客网 时间:2024/06/05 07:44
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> list = new ArrayList<Integer>(); if(matrix.length==0){ return list; } int m = matrix.length; int n = matrix[0].length; int s = 0; while(list.size()<matrix.length*matrix[0].length){ for(int i=s;i<n;i++){ list.add(matrix[s][i]); } for(int i=s+1;i<m;i++){ list.add(matrix[i][n-1]); } if(s!=m-1){ for(int i=n-2;i>s-1;i--){ list.add(matrix[m-1][i]); } } if(s!=n-1){ for(int i=m-2;i>s;i--){ list.add(matrix[i][s]); } } s++; m--; n--; } return list; }}
阅读全文
0 0
- LeetCode 54. Spiral Matrix
- [LeetCode]54.Spiral Matrix
- LeetCode --- 54. Spiral Matrix
- [Leetcode] 54. Spiral Matrix
- [leetcode] 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
- leetcode 54. Spiral Matrix
- LeetCode *** 54. Spiral Matrix
- [LeetCode]54. Spiral Matrix
- LeetCode 54. Spiral Matrix
- LeetCode--54. Spiral Matrix
- LeetCode - 54. Spiral Matrix
- [Leetcode]54. Spiral Matrix
- vue 中报错 __webpack_require__.i(…) is not a function
- wifi一键配网smartconfig原理及应用
- 由浅入深学习Apache httpd原理与配置
- webstrom配置移动端本地测试
- Java代理模式 动态代理 的应用场景
- leetcode 54. Spiral Matrix
- Sass/Less对比
- Android 4.1~4.3 Jelly Bean
- 网站页面添加
- 微信小程序周报(第五期)
- 驱动版本的i2cdetect
- 我在 linux 踩的坑
- Android 4.4
- 【Java面试】---泛型