[LeetCode] 54. Spiral Matrix 螺旋矩阵
LeetCode 矩阵 matrix 54 螺旋
2023-09-27 14:28:37 时间
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]
.
将一个矩阵按螺旋顺序输出。
解法:用变量left, right, top, bottom记录左,右,顶,底。然后按照左到右,顶到底,右到左,底到顶的顺序循环,把遍历的元素加入到结果。
Java:
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> res = new ArrayList<>(); if(matrix == null || matrix.length == 0) return res; int rowNum = matrix.length, colNum = matrix[0].length; int left = 0, right = colNum - 1, top = 0, bot = rowNum - 1; while(res.size() < rowNum * colNum) { for(int col = left; col <= right; col++) res.add(matrix[top][col]); top++; if(res.size() < rowNum * colNum) { for(int row = top; row <= bot; row++) res.add(matrix[row][right]); right--; } if(res.size() < rowNum * colNum) { for(int col = right; col >= left; col--) res.add(matrix[bot][col]); bot--; } if(res.size() < rowNum * colNum) { for(int row = bot; row >= top; row--) res.add(matrix[row][left]); left++; } } return res; } }
Python:
class Solution: # @param matrix, a list of lists of integers # @return a list of integers def spiralOrder(self, matrix): result = [] if matrix == []: return result left, right, top, bottom = 0, len(matrix[0]) - 1, 0, len(matrix) - 1 while left <= right and top <= bottom: for j in xrange(left, right + 1): result.append(matrix[top][j]) for i in xrange(top + 1, bottom): result.append(matrix[i][right]) for j in reversed(xrange(left, right + 1)): if top < bottom: result.append(matrix[bottom][j]) for i in reversed(xrange(top + 1, bottom)): if left < right: result.append(matrix[i][left]) left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1 return result
C++:
class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> res; if (matrix.empty() || matrix[0].empty()) return res; int m = matrix.size(), n = matrix[0].size(); int c = m > n ? (n + 1) / 2 : (m + 1) / 2; int p = m, q = n; for (int i = 0; i < c; ++i, p -= 2, q -= 2) { for (int col = i; col < i + q; ++col) res.push_back(matrix[i][col]); for (int row = i + 1; row < i + p; ++row) res.push_back(matrix[row][i + q - 1]); if (p == 1 || q == 1) break; for (int col = i + q - 2; col >= i; --col) res.push_back(matrix[i + p - 1][col]); for (int row = i + p - 2; row > i; --row) res.push_back(matrix[row][i]); } return res; } };
All LeetCode Questions List 题目汇总
相关文章
- LeetCode:搜索二维矩阵题解
- 【LeetCode】至少有 K 个重复字符的最长子串 [M](滑动窗口)
- 【LeetCode】生命游戏 [M](矩阵)
- 【LeetCode】矩阵置零 [M](矩阵)
- 【LeetCode】矩阵中的最长递增路径 [H](记忆化搜索)
- leetcode:92. 反转链表 II(C++)
- 矩阵中和能被 K 整除的路径 leetcode第314周赛第四题
- LeetCode 232:Implement Queue using Stacks
- LeetCode_贪心算法_简单_455.分发饼干
- LeetCode_排序_快速选择_中等_462.最小操作次数使数组元素相等 II
- LeetCode_动态规划_中等_673.最长递增子序列的个数
- LeetCode_滑动窗口_哈希表_中等_187. 重复的DNA序列
- LeetCode_螺旋遍历_中等_59.螺旋矩阵 II
- leetcode 73. 矩阵置零
- LeetCode·每日一题·2341.数组能形成多少数对·排序
- LeetCode·435.无重叠区间·贪心
- LeetCode·301.删除无效的括号·BFS
- LeetCode No3. 无重复字符的最长子串 题解
- 「LeetCode」61. 旋转链表
- LeetCode-38. 外观数列(Goland实现)
- [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵
- leetcode 54 螺旋矩阵
- leetcode 343 整数拆分
- leetcode 968 监控摄像头
- leetcode 226 翻转二叉树