Array-----54. Spiral Matrix(蛇形填矩阵)
来源:互联网 发布:金坷垃淘宝 编辑:程序博客网 时间:2024/06/09 17:15
原题目
看到通过率比二还低就做了,其实思路基本一样,主要是要记录一个数据填了多少,超出的时候退出循环。
public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result = new ArrayList<>(); if (matrix == null || matrix.length == 0) { return result; } int m = matrix.length; int n = matrix[0].length; int top = 0; int bottom = m - 1; int left = 0; int right = n - 1; int k = 0; while (k < m * n) { for (int i = left; i <= right && k < m * n; i++) { result.add(matrix[top][i]); k++; } top++; for (int i = top; i <= bottom && k < m * n; i++) { result.add(matrix[i][right]); k++; } right--; for (int i = right; i >= left && k < m * n; i--) { result.add(matrix[bottom][i]); k++; } bottom--; for (int i = bottom; i >= top && k < m * n; i--) { result.add(matrix[i][left]); k++; } left++; } return result; }
阅读全文
0 0
- Array-----54. Spiral Matrix(蛇形填矩阵)
- Array-----59. Spiral Matrix II(蛇形打印矩阵)
- leetcode-54. Spiral Matrix(打印蛇形矩阵)
- leetcode54-Spiral Matrix(打印蛇形矩阵)
- leetcode59-Spiral Matrix II(生成蛇形矩阵)
- LeetCode 54 Spiral Matrix (模拟 蛇形填数)
- 蛇形螺旋矩阵的生成和遍历 Spiral Matrix
- LeetCode:蛇形矩阵II(spiral matrix II)
- leetcode.array--54. Spiral Matrix
- 54. Spiral Matrix 螺旋矩阵
- PAT (Advanced Level) Practise 1105. Spiral Matrix (25) 蛇形填数
- LeetCode 54 Spiral Matrix(螺旋矩阵)(Array)(*)
- LeetCode[Array]: Spiral Matrix
- LeetCode 54. Spiral Matrix(螺旋矩阵)
- 【矩阵】Spiral Matrix
- Spiral Matrix 螺旋矩阵
- NYOJ_33 蛇形填数(蛇形矩阵)
- 蛇形填数(矩阵)
- 系统学习 jQuery (四) DOM 操作
- 33_联表操作
- Java实践(四)---关于clone
- C++11 : unique_ptr
- RecyclerView的多种布局MultiItemTypeAdapter
- Array-----54. Spiral Matrix(蛇形填矩阵)
- 34_分页处理
- [Leetcode] 298. Binary Tree Longest Consecutive Sequence 解题报告
- 短路求值(Short-circuit evaluation)
- 草木有本心,何求美人折
- 微信小程序开发 | 来自小程序开发者的开发教程②
- Python开发环境搭建和配置
- Cookie禁用了,Session还能用吗?
- Python函数之 ceil, floor,round