LeetCode-54. Spiral Matrix (JAVA)(顺时针打印矩阵)
来源:互联网 发布:淘宝返利网站哪个最好 编辑:程序博客网 时间:2024/06/18 00:31
54. Spiral Matrix
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
public List<Integer> spiralOrder(int[][] arr) {List<Integer> res = new ArrayList<>();if (arr.length == 0)return res;int rows = arr.length;int cols = arr[0].length;// 此种方法关键点--求圈数// 最小值除以2,向上取整// int layers = (Math.min(rows, cols) - 1) / 2 + 1;int layers = (int) Math.ceil((Math.min(rows, cols)) / 2.0);// 要打印的圈数for (int i = 0; i < layers; i++) {// 打印每圈// 左至右for (int k = i; k < cols - i; k++)res.add(arr[i][k]);// 右上至右下for (int j = i + 1; j < rows - i; j++)res.add(arr[j][cols - i - 1]);// 注意k,j开始的下标// 右至左// (rows - i - 1 != i)避免重复打印第i行for (int k = cols - i - 2; (k >= i) && (rows - i - 1 != i); k--)res.add(arr[rows - i - 1][k]);// 左下至左上// (cols - i - 1 != i)避免重复打印第i列for (int j = rows - i - 2; (j > i) && (cols - i - 1 != i); j--)res.add(arr[j][i]);}return res;}
discuss解法
The only tricky part is that when I traverse left or up I have to check whether the row or col still exists to prevent duplicates
(往左还是往上遍历 的时候,要检查是否row和col已经存在,避免重复打印(加入if判断))
public List<Integer> spiralOrder(int[][] matrix) {List<Integer> res = new ArrayList<Integer>();if (matrix.length == 0) {return res;}int up = 0;int down = matrix.length - 1;int left = 0;int right = matrix[0].length - 1;// 取到‘=’是因为走过的已经移动指针while (up <= down && left <= right) {// Traverse Right(→)for (int j = left; j <= right; j++) {res.add(matrix[up][j]);}up++;// Traverse Down(↓)for (int j = up; j <= down; j++) {res.add(matrix[j][right]);}right--;if (up <= down) {// Traverse Left(←)for (int j = right; j >= left; j--) {res.add(matrix[down][j]);}}down--;if (left <= right) {// Traver Up(↑)for (int j = down; j >= up; j--) {res.add(matrix[j][left]);}}left++;}return res;}
0 0
- LeetCode-54. Spiral Matrix (JAVA)(顺时针打印矩阵)
- LeetCode | Spiral Matrix(顺时针打印矩阵)
- 牛客:剑指offer:顺时针打印矩阵 (Java)(同leetcode的spiral matrix i 和ii)
- leetcode-54 Spiral Matrix 顺时针打印矩阵(《剑指offer》面试题20)
- Spiral Matrix所谓的顺时针打印矩阵
- leetcode-54. Spiral Matrix(打印蛇形矩阵)
- leetcode 54 54. Spiral Matrix(矩阵顺时针绕圈输出)
- LeetCode 54. Spiral Matrix(螺旋矩阵)
- Leetcode 54 - Spiral Matrix(螺旋打印矩阵)
- [LeetCode] Spiral Matrix 旋转打印矩阵
- LeetCode------54. Spiral Matrix(螺旋二维矩阵)-----59. Spiral Matrix II(n的螺旋矩阵)
- leetcode54-Spiral Matrix(打印蛇形矩阵)
- LeetCode 59. Spiral Matrix II (JAVA)(螺旋矩阵2)
- 剑指offer 面试题20:顺时针打印矩阵及其变形(LeetCode54. Spiral Matrix旋转矩阵) 题解
- leetcode-java-54. Spiral Matrix
- (Java)LeetCode-54. Spiral Matrix
- [leetcode]54. Spiral Matrix(Java)
- leetcode 59 Spiral Matrix II 螺旋矩阵的打印
- Breakpoints:An Analysis of Potential Hypervisor Attack vectors (断点:潜在的管理程序分析攻击变量)
- ASN.1探索 - 3 编码规则与传输语法(1
- xaml是什么
- 多选框checkbox传值给后台用string,和string[]接收效果不同
- 【BZOJ 3105】【CQOI 2013】新Nim游戏
- LeetCode-54. Spiral Matrix (JAVA)(顺时针打印矩阵)
- mybatis等框架调用存存过程
- dwz的跳转刷新
- 导出,下载,(如何决定下不下载)
- Android webview的销毁和清除历史记录
- 官网生成带Zero的Abp出现Default language is not defined!的错误
- dwz的select下拉框不为空判断
- 关于angular.js基础学习
- 更新设置,session,token(PC,APP)