1、解题思路
- 初始化边界:设定矩阵的上下左右边界(top, bottom, left, right)。
- 按层遍历 : 从左到右遍历上层边界(top层)。从上到下遍历右层边界(right层)。从右到左遍历下层边界(bottom层)。从下到上遍历左层边界(left层)。
- 更新边界:每完成一层遍历后,收缩相应的边界。
- 终止条件:当所有边界相遇时,遍历完成。
这种方法确保我们按照螺旋顺序访问矩阵中的所有元素。
2、代码实现
C++
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param matrix int整型vector<vector<>> * @return int整型vector */ vector<int> spiralOrder(vector<vector<int> >& matrix) { // write code here vector<int> result; if (matrix.empty() || matrix[0].empty()) { return result; } int top = 0, bottom = matrix.size() - 1; int left = 0, right = matrix[0].size() - 1; while (top <= bottom && left <= right) { // 从左到右遍历上层边界 for (int i = left; i <= right; ++i) { result.push_back(matrix[top][i]); } top++; // 从上到下遍历右层边界 for (int i = top; i <= bottom; ++i) { result.push_back(matrix[i][right]); } right--; // 检查是否还有下层边界需要遍历 if (top <= bottom) { // 从右到左遍历下层边界 for (int i = right; i >= left; --i) { result.push_back(matrix[bottom][i]); } bottom--; } // 检查是否还有左层边界需要遍历 if (left <= right) { // 从下到上遍历左层边界 for (int i = bottom; i >= top; --i) { result.push_back(matrix[i][left]); } left++; } } return result; } };
Java
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param matrix int整型二维数组 * @return int整型ArrayList */ public ArrayList<Integer> spiralOrder (int[][] matrix) { // write code here ArrayList<Integer> result = new ArrayList<>(); if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return result; } int top = 0, bottom = matrix.length - 1; int left = 0, right = matrix[0].length - 1; while (top <= bottom && left <= right) { // 从左到右遍历上层边界 for (int i = left; i <= right; ++i) { result.add(matrix[top][i]); } top++; // 从上到下遍历右层边界 for (int i = top; i <= bottom; ++i) { result.add(matrix[i][right]); } right--; // 检查是否还有下层边界需要遍历 if (top <= bottom) { // 从右到左遍历下层边界 for (int i = right; i >= left; --i) { result.add(matrix[bottom][i]); } bottom--; } // 检查是否还有左层边界需要遍历 if (left <= right) { // 从下到上遍历左层边界 for (int i = bottom; i >= top; --i) { result.add(matrix[i][left]); } left++; } } return result; } }
Python
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param matrix int整型二维数组 # @return int整型一维数组 # class Solution: def spiralOrder(self , matrix: List[List[int]]) -> List[int]: # write code here if not matrix or not matrix[0]: return [] result = [] top, bottom = 0, len(matrix) - 1 left, right = 0, len(matrix[0]) - 1 while top <= bottom and left <= right: # 从左到右遍历上层边界 for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 # 从上到下遍历右层边界 for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 # 检查是否还有下层边界需要遍历 if top <= bottom: # 从右到左遍历下层边界 for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 # 检查是否还有左层边界需要遍历 if left <= right: # 从下到上遍历左层边界 for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result
3、复杂度分析
- 边界初始化:设定矩阵的上下左右边界(top, bottom, left, right)。
- 按层遍历 : 从左到右遍历上层边界。从上到下遍历右层边界。从右到左遍历下层边界。从下到上遍历左层边界。
- 边界收缩:每次遍历完一层后,收缩相应的边界。
- 终止条件:当所有边界相遇时,遍历完成。
- 时间复杂度:O(nm),因为每个元素被访问一次。
- 空间复杂度:O(nm),用于存储结果。