【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】
来源:互联网 发布:如何网络招生 编辑:程序博客网 时间:2024/06/05 01:59
【054-Spiral Matrix(螺旋矩阵)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
原题
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]
.
题目大意
给定一个m*n的矩阵,输入所有元素的螺旋顺序。
解题思路
使用计算输出的方法,先处理上面一行,再处理右边一列,再处理下面一行,再处理左边一列,一直这样操作,直到所有的元素都处理完。
代码实现
算法实现类
import java.util.ArrayList;import java.util.List;public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result = new ArrayList<>(50); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return result; } // 只有一行的情况 if (matrix.length == 1) { for (int i : matrix[0]) { result.add(i); } return result; } // 只有一列的情况 if (matrix[0].length == 1) { for (int i = 0; i < matrix.length; i++) { result.add(matrix[i][0]); } return result; } // 计算有多少圈 int row = matrix.length; int col = matrix[0].length; int cycle = row < col ? row : col; cycle = (cycle + 1) / 2; int round = 0; // 记录当前是第几圈 int left = 0; int right = matrix[0].length - 1; int top = 0; int down = matrix.length - 1; int total = col*row; int count = 0; while (round < cycle) { // 上面一行 for (int i = left; i <= right && count < total; i++) { count++; result.add(matrix[round][i]); } top++; // // 右边一列 for (int i = top; i <= down && count < total; i++) { count++; result.add(matrix[i][col - round - 1]); } right--; // 底下一行 for (int i = right; i >= left && count < total; i--) { count++; result.add(matrix[row - round - 1][i]); } down--; // 左边一列 for (int i = down; i >= top && count < total; i--) { count++; result.add(matrix[i][round]); } left++; round++; } return result; }}
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47120491】
2 0
- 【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
- LeetCode 59. Spiral Matrix II (JAVA)(螺旋矩阵2)
- LeetCode 54. Spiral Matrix(螺旋矩阵)
- LeetCode Spiral Matrix(螺旋矩阵)
- LeetCode------54. Spiral Matrix(螺旋二维矩阵)-----59. Spiral Matrix II(n的螺旋矩阵)
- [算法][LeetCode]Spiral Matrix——螺旋矩阵
- 【LeetCode-面试算法经典-Java实现】【074-Search a 2D Matrix(搜索二维矩阵)】
- 【LeetCode-面试算法经典-Java实现】【070-Set Matrix Zeroes(矩阵置零)】
- LeetCode 54 Spiral Matrix(螺旋矩阵)(Array)(*)
- LeetCode | Spiral Matrix II(螺旋矩阵填充数据)
- LeetCode OJ 之 Spiral Matrix (螺旋矩阵)
- LeetCode OJ 之 Spiral Matrix II (螺旋矩阵 - 二)
- LeetCode 59. Spiral Matrix II(螺旋矩阵)
- Leetcode 54 spiral matrix II (螺旋矩阵)
- Leetcode 54 - Spiral Matrix(螺旋打印矩阵)
- 《leetcode》spiral-matrix-ii(构造螺旋矩阵)
- [LeetCode] Spiral Matrix 螺旋输出矩阵
- VS2015 卡在 LocalESPCui for zh-cn Dev12
- Leetcode NO.139 Word Break
- [BZOJ1634] [Usaco2007 Jan]Protecting the Flowers 护花
- 7-28学习总结
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
- 【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】
- 【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】
- 常用设计模式
- [BZOJ1001] [BeiJing2006]狼抓兔子
- 愚人节的礼物
- Train Problem I
- 好好学习ios,天天向上
- 神奇 2013.1080P 国语中字[黄晓明 郭采洁主演]
- Java中使用md5生成摘要